且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用JavaScript正则表达式匹配和替换字符串

更新时间:2022-11-14 23:35:38

它是这样的:

   > html = "This is HTML<br><style>#a{}</style>This is more HTML.<style>#b{}</style>"
   > tags = []
   > html.replace(/<.+?>/g, function(match) { tags.push(match); return "" })
   "This is HTML#a{}This is more HTML.#b{}"
   > tags
   ["<br>", "<style>", "</style>", "<style>", "</style>"]

(关于正则表达式不适合解析标记语言的强制性通知).

(An obligatory notice about regexes not being suitable for parsing markup languages).