说明
<\/?(?!img)[a-z]+(?=[\s>])(?:[^>=]|=(?:'[^']*'|"[^"]*"|[^'"\s]*))*\s?\/?>
替换为: nothing
此正则表达式将执行以下操作:
- 查找所有打开和关闭的 html 标签
- 忽略
img标签
- 避免使 HTML 中的模式匹配变得困难的困难边缘情况
示例
现场演示
https://regex101.com/r/sI2nO0/3
示例文本
注意嵌套在第一个锚标记内的困难边缘情况。
<span><a onmouseover=' if ( 3 > a ) { var
string=" <img src=NotTheDroidYouAreLookingFor.jpg>; "; } '
href="link.html">This is a droid I'm looking
for: <img src=DesiredDroids.png></a>
</span>
更换后
This is a droid I'm looking
for: <img src=DesiredDroids.png>
说明
NODE EXPLANATION
----------------------------------------------------------------------
< '<'
----------------------------------------------------------------------
\/? '/' (optional (matching the most amount
possible))
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
img 'img'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
[a-z]+ any character of: 'a' to 'z' (1 or more
times (matching the most amount possible))
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
[\s>] any character of: whitespace (\n, \r,
\t, \f, and " "), '>'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
----------------------------------------------------------------------
[^>=] any character except: '>', '='
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
= '='
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
' '\''
----------------------------------------------------------------------
[^']* any character except: ''' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
' '\''
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
" '"'
----------------------------------------------------------------------
[^"]* any character except: '"' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
" '"'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[^'"\s]* any character except: ''', '"',
whitespace (\n, \r, \t, \f, and " ")
(0 or more times (matching the most
amount possible))
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
\s? whitespace (\n, \r, \t, \f, and " ")
(optional (matching the most amount
possible))
----------------------------------------------------------------------
\/? '/' (optional (matching the most amount
possible))
----------------------------------------------------------------------
> '>'
----------------------------------------------------------------------