JavaScript正则表达式中的前瞻 [英] Lookahead in JavaScript regex

查看:97
本文介绍了JavaScript正则表达式中的前瞻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaScript,尝试替换html标记内的属性,并提出了此正则表达式:

Using JavaScript, I am trying to replace an attribute inside an html tag, and have come up with this regex:

/<\s*tag[^>]*(attr)=['"]{1,1}([^'"\s]*)['"]{1,1}/ig;

这有效.但是,我希望能够指定查找包含属性值的相同类型的引号.因此,例如,我想指定这是否为<tag attr='data'>形式,以便在SECOND引号中查找单引号,而不是双引号.相反的情况,<tag attr="data">将是相似的.用双引号(而不是单引号)匹配SECOND标记.这是为了帮助我保护函数调用免受结构异常的HTML攻击.

This works. However, I want to be able to specify to look for the same type of quotation mark enclosing the attribute value. So, for example, I want to specify if this is the form <tag attr='data'>, to look in the SECOND quotation mark for the single one, not the double one. The inverse case, <tag attr="data"> would be similar; match the SECOND mark with double quotes, not single ones. This is to help me protect the function call against strangely formed HTML.

那么,我该如何实现呢?

So, how can I achieve this?

谢谢!

推荐答案

尝试一下:

/<tag[^>]*attr=(['"])(?:(?!\1)\S)*\1/ig;

说明:

<tag     # Match <tag (\s* is not needed since whitespace is illegal here)
[^>]*    # Match any non-> characters
attr=    # Match "attr="
(['"])   # Match a quote, remember which kind; {1,1} can be dropped (it's a no-op)
(?:      # Try to match
 (?!\1)  #  (unless it's the corresponding closing quote)
 \S      #  any non-whitespace character
)*       # any number of times
\1       # Match the corresponding closing quote

这篇关于JavaScript正则表达式中的前瞻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆