我不会使用正则表达式来解析标记,但如果它只是一个字符串片段,这样的东西就足够了。应该指出,你使用的正则表达式使用\ s *来负担过重。它的可选形式可以通过开销并替换完全相同的东西。最好使用\ S +
正则表达式:<(/?(?:b|i|u)|code\s[^>]+class\s*=\s*(['"]).*?\2[^>]*?)\s+>
取代:<$1>
修饰符:sgi
< # < Opening markup char
( # Capture group 1
/? # optional element termination
(?: # grouping, non-capture
b|i|u # elements 'b', 'i', or 'u'
) # end grouping
| # OR,
code # element 'code' only
\s [^>]* # followed by a space and possibly any chars except '>'
class \s* = \s* # 'class' attribute '=' something
(['"]) .*? \2 # value delimeter, then some possible chars, then delimeter
[^>]*? # followed by possibly any chars not '>'
) # End capture group 1
\s+ # Here need 1 or more whitespace, what is being removed
> # > Closing markup char
来源
2011-03-14 18:52:53
sln
不要使用正则表达式解析HTML/XML。为什么不使用jQuery的操纵器呢? – 2011-03-14 17:45:02