如何组合这两种正则表达式模式?

der*_*271 5 regex

我不得不问这个问题感觉很傻,但我不能让这个来拯救我的生命......

什么有用

preg_replace( '/(<[^>]+) onmouseout=".*?"/i', '$1', preg_replace( '/(<[^>]+) onmouseover=".*?"/i', '$1', $strHtml ) )
Run Code Online (Sandbox Code Playgroud)

如何将这两个preg_replace函数合并为一个(通过梳理两个正则表达式模式?

我的尝试清理(不起作用)

preg_replace( '/(<[^>]+) (onmouseover|onmouseout)=".*?"/i', '$1', $strHtml )
Run Code Online (Sandbox Code Playgroud)

我希望此preg_replace()函数从我的HTML字符串中删除所有onmouseoverAND onmouseout属性.它似乎只删除了两个属性中的一个......我做错了什么?

更新:示例字符串

<p><img src="http://www.bestlinknetware.com/products/204233spc.jpg" width="680" height="365"><br>   <a href="http://www.bestlinknetware.com/products/204233INST.pdf" target="_blank" onmouseover="MM_swapImage('Image2','','/Content/bimages/ins2.gif',1)" onmouseout="MM_swapImgRestore()"><img name="Image2" border="0" src="http://www.bestlinknetware.com/Content/bimages/ins1.gif"></a> </p> <p><strong>No contract / No subscription / No monthy fee<br> 1080p HDTV reception<br> 32db high gain reception<br> Rotor let you change direction of the antenna to find best reception</strong></p>  <a href=http://transition.fcc.gov/mb/engineering/dtvmaps/  target="blank"><strong>CLICK HERE</strong></a><br>to see HDTV channels available in your area.<br> <br/> ** TV signal reception is immensely affected by the conditions such as antenna height, terrain, distance from broadcasting transmission antenna and output power of transmitter. Channels you can watch may vary depending on these conditions. <br> <br/> <br/> <p>* Reception: VHF/UHF/FM<br/>   * Reception range: 120miles<br/>   * Built-in 360 degree motor rotor<br>   * Wireless remote controller for rotor (included)<br/>   * Dual TV Outputs<br>   * Easy Installation<br>   * High Sensitivity Reception<br>   * Built-in Super Low Noise Amplifier<br>   * Power : AC15V 300mA<br> <br/> Kit contents<br/> * One - HDTV Yagi antenna with built-in roter & amplifier<br/> * One - Roter control box<br/> * One - Remote for roter control box<br/> * One - 40Ft coax cable<br/> * One - 4Ft coax cable<br/> * One - power supply for roter control box</p>
Run Code Online (Sandbox Code Playgroud)

更新:此线程的未来视图的工具

https://regex101.com/

我永远无法弄清楚如何使用http://regexr.com/,所以我尝试了这个regex101.com网站,从那时起我一直很喜欢它.强烈建议任何面临类似问题的人(使用像我原来那样的剪切和粘贴正则表达式模式......).

sha*_*t00 1

你原来的表达的问题是,最初的一组抓住了太多,所以两个被替换的唯一一个是最后出现的那个。发生这种情况是因为贪婪的[^>]+重复占用了比您预期更大的搜索字符串部分,捕获了从第一个所需匹配的开头到您想要删除的第二个属性的开头的所有内容。然后,即使在解决了该问题之后,将模式锚定到 html 标记的起始括号也会阻止元素内的多个匹配。

如果您想在一次调用 then 中执行此操作,preg_replace()而不是尝试获取要保留的文本,则查找要删除的文本(通过用空字符串替换)更有意义:

preg_replace( '/(onmouseover|onmouseout)=".*?"/i', '', $strHtml )
Run Code Online (Sandbox Code Playgroud)

您已经对属性值进行了非贪婪匹配(使用.*?),并且根据您之前的代码,它似乎已经对您运行良好。请注意,这个特定的表达式并不涵盖 HTML/XML 文档中所有可能的变体(例如空格和引号)。我相信您可以判断这是否足够通用以满足您的需要。