※ 本文轉寄自 tomin.bbs@tomin.twbbs.org
標題 strip all html tags
時間 2010/11/09 Tue 14:27:52
Using a Regular Expression to Match HTML
http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx
最直覺的寫法是
</?\w+\s+[^>]*>
Jeffrey Friedl的解法
</?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)/?>
可以拆解,會比較好看懂
</?\w+
(
(\s+\w+
(\s*=\s*
(?: ".*?" | '.*?' | [^'">\s]+)
)?
)
+\s*|\s*
)
/?>
為了能在js上跑,我加上escape,
為了再移除<meta http-equiv="Content-Type">的第一個dash 修改一下成為:
/<\/?\w+((\s+[^-][\w-]+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>/g
再來是移除註解
.replace(/<!--(\n|.)*?-->/g,"");
完成
--
--
看板 coding
作者 標題 strip all html tags
時間 2010/11/09 Tue 14:27:52
Using a Regular Expression to Match HTML
http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx
最直覺的寫法是
</?\w+\s+[^>]*>
Jeffrey Friedl的解法
</?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)/?>
可以拆解,會比較好看懂
</?\w+
(
(\s+\w+
(\s*=\s*
(?: ".*?" | '.*?' | [^'">\s]+)
)?
)
+\s*|\s*
)
/?>
為了能在js上跑,我加上escape,
為了再移除<meta http-equiv="Content-Type">的第一個dash 修改一下成為:
/<\/?\w+((\s+[^-][\w-]+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>/g
再來是移除註解
.replace(/<!--(\n|.)*?-->/g,"");
完成
--
◤◥ Origin: Loess Plateau˙黃土高原 tomin.twbbs.org
◣◢ Author: tomin 從 tomin.mdorm.ntnu.edu.tw 發表
--
※ 看板: tomin 文章推薦值: 0 目前人氣: 0 累積人氣: 22
回列表(←)
分享