首页 新闻 会员 周边

求大神帮忙看一个检测html标签正则表达式的问题

0
悬赏园豆:100 [待解决问题]

问题是酱紫的:

/<(\/\s*)?((\w+:)?\w+)(\w+(\s*=\s*((["'])(\\["'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>/.test("Ask God to help <look at the issue of a large regular expression help! help! help!")

直接运行代码,发现执行效率异常的慢,浏览器直接卡死,任务管理器查看发现一直在占用内存。这个正则表达式的作用是检测文本中是否含有html标签,现在是测试一个正常输入一个小于号的情况,悲剧了

咖啡+的主页 咖啡+ | 初学一级 | 园豆:102
提问于:2014-11-27 16:37
< >
分享
所有回答(4)
0

@"<(!|/)?\w+( ((.|\n)*?"")?)? *>"

Jaryleely | 园豆:367 (菜鸟二级) | 2014-11-27 16:42
0

<abc> 这种算不算?

Yu | 园豆:12980 (专家六级) | 2014-11-27 17:13

算的 就是<>不能成对的出现

支持(0) 反对(0) 咖啡+ | 园豆:102 (初学一级) | 2014-11-27 17:16

@咖啡+: 

 

<([^<]*?)>(.*?)</\1>

支持(0) 反对(0) Yu | 园豆:12980 (专家六级) | 2014-11-27 17:21

@Yu: 

$(document).ready(
    function() {
        var r=/<([^<]*?)>(.*?)<\/\1>/.test("Ask God to help <look>help! help!</look>");

        alert(r);
    });
支持(0) 反对(0) Yu | 园豆:12980 (专家六级) | 2014-11-27 17:23

@Yu: 这样不够的 这个正则表达式主要的作用就是过滤用户输入html标签,防止注入

/<([^<]*?)>(.*?)<\/\1>/.test("Ask God to help <span class='title'>help! help!</span>") 这样的过了

支持(0) 反对(0) 咖啡+ | 园豆:102 (初学一级) | 2014-11-27 17:47

@咖啡+: 

 

<([^<]*?)(\s+[^<]*)?>(.*?)<\/\1>

支持(0) 反对(0) Yu | 园豆:12980 (专家六级) | 2014-11-27 20:14
0

/<(\w*).*>.*<\/\1>/.test("Ask God to help <span class='title'>help! help!</span>")

ThreeTree | 园豆:1490 (小虾三级) | 2014-11-27 20:19

/<(\w*).*>.*<\/\1>/.test("Ask God to help span<img> class='title' help! help!") 这种情况也不能让用户输入

支持(0) 反对(0) 咖啡+ | 园豆:102 (初学一级) | 2014-11-28 09:14

@咖啡+: /<.*?>/.test("Ask God to help span<img> class='title' help! help!")

支持(0) 反对(0) ThreeTree | 园豆:1490 (小虾三级) | 2014-11-28 11:23
0
正则表达式 <(\S*?) [^>]*>.*?</\1>|<.*? />
匹配 <html>hello</html>|<a>abcd</a>
不匹配 abc|123|<html>ddd

 

正则表达式 ^[^<>`~!/@\#}$%:;)(_^{&*=|'+]+$
匹配 This is a test
不匹配 <href = | <br> | That's it

 

正则表达式 &lt;!--.*?--&gt;
匹配 &lt;!-- &lt;h1&gt;this text has been removed&lt;/h1&gt; --&gt; | &lt;!-- yada --&gt;
不匹配 &lt;h1&gt;this text has not been removed&lt;/h1&gt;

 

正则表达式 (\[(\w+)\s*(([\w]*)=('|&quot;)?([a-zA-Z0-9|:|\/|=|-|.|\?|&amp;]*)(\5)?)*\])([a-zA-Z0-9|:|\/|=|-|.|\?|&amp;|\s]+)(\[\/\2\])
匹配 [link url=&quot;http://www.domain.com/file.extension?getvar=value&amp;secondvar=value&quot;]Link[/li
不匹配 [a]whatever[/b] | [a var1=something var2=somethingelse]whatever[/a] | [a]whatever[a]

 

正则表达式 href=[\"\'](http:\/\/|\.\/|\/)?\w+(\.\w+)*(\/\w+(\.\w+)?)*(\/|\?\w*=\w*(&\w*=\w*)*)?[\"\']
匹配 href="www.yahoo.com" | href="http://localhost/blah/" | href="eek"
不匹配 href="" | href=eek | href="bad example"

 

正则表达式 &quot;([^&quot;](?:\\.|[^\\&quot;]*)*)&quot;
匹配 &quot;This is a \&quot;string\&quot;.&quot;
不匹配 &quot;This is a \&quot;string\&quot;.

 

正则表达式 (?i:on(blur|c(hange|lick)|dblclick|focus|keypress|(key|mouse)(down|up)|(un)?load|mouse(move|o(ut|ver))|reset|s(elect|ubmit)))
匹配
  onclick | onsubmit | onmouseover
不匹配 click | onandon | mickeymouse

 

正则表达式 (?s)/\*.*\*/
匹配 /* .................... */ | /* imagine lots of lines here */
不匹配 */ malformed opening tag */ | /* malformed closing tag /*

 

正则表达式 <(\S*?) [^>]*>.*?</\1>|<.*? />
匹配 <html>hello</html>|<a>abcd</a>
不匹配 abc|123|<html>ddd

 

正则表达式 \xA9
匹配 ©
不匹配 anything

 

正则表达式 src[^&gt;]*[^/].(?:jpg|bmp|gif)(?:\&quot;|\')
匹配 src=&quot;../images/image.jpg&quot; | src=&quot;http://domain.com/images/image.jpg&quot; | src='d:\w
不匹配 src=&quot;../images/image.tif&quot; | src=&quot;cid:value&quot;

 

正则表达式 /\*[\d\D]*?\*/
匹配 /* my comment */ | /* my multiline comment */ | /* my nested comment */
不匹配 */ anything here /* | anything between 2 seperate comments | \* *\

 

正则表达式 <[a-zA-Z]+(\s+[a-zA-Z]+\s*=\s*("([^"]*)"|'([^']*)'))*\s*/>
匹配 <img src="test.gif"/>
不匹配 <img src="test.gif"> | <img src="test.gif"a/>
Mr.Brian | 园豆:1518 (小虾三级) | 2014-11-28 09:30

个人觉得放在服务端验证更好做吧。

支持(0) 反对(0) Mr.Brian | 园豆:1518 (小虾三级) | 2014-11-28 09:31

@Mr.Brian: 服务端的验证做的比前端弱,这个是个坑

支持(0) 反对(0) 咖啡+ | 园豆:102 (初学一级) | 2014-11-28 09:35

@咖啡+: 奥,那你试试前面那几个有没有适合你的,这东西比较费事,是细节性东西。静下心来自己多尝试尝试!不要太着急!

支持(0) 反对(0) Mr.Brian | 园豆:1518 (小虾三级) | 2014-11-28 09:42

@Mr.Brian:谢谢! 其实回到我提出的这个问题的初衷上来,我给出的这个正则表达式是可以满足要求的,但是就是碰到字符串超过100个左右的情况下,效率的显得特别慢,会直接把浏览器卡死,所以能帮我直接分析一下上面正则表达式的性能是最好的。

支持(0) 反对(0) 咖啡+ | 园豆:102 (初学一级) | 2014-11-28 09:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册