JS 正则表达式中小括号的应用

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了JS 正则表达式中小括号的应用脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

主要使用的有下面三种:

1.  (...)

Grouping. Group ITems into a single unit that can be used with *, +, ?, |, and so on. Also remember the characters that match this group for use with later references.

2. (?:...)

Grouping only. Group items into a single unit, but do not remember the characters that match this group.

3.

(?=pA positive lookahead assertion. 或

(?!p)  A negative lookahead assertion.

 

用于测试的字符串:url(skins/default/images/index/default.png)

红色字符为匹配上的结果

第一种,用于普通分组,能记住匹配该分组的字符串,并且可以在以后通过\1的方式来引用所匹配到的分组

       eg: (\/)匹配url(skins/default/images/index/default.png)

           (\/)default\1匹配url(skins/default/images/index/default.png)

 

第二种,用于分组,不记录匹配该分组的字符串

       eg: (?:\/)default匹配url(skins/default/images/index/default.png)

 

第一、二两种方式匹配的结果里都包含有该分组匹配到的结果,在例子中是"/default"中的"/"

 

第三种,只用于确定位置,最终结果里并不包含该括号匹配到的结果

       eg:  (?!\/)default匹配url(skins/default/images/index/default.png)

       eg:  default(?=\/)匹配url(skins/default/images/index/default.png)

脚本宝典总结

以上是脚本宝典为你收集整理的JS 正则表达式中小括号的应用全部内容,希望文章能够帮你解决JS 正则表达式中小括号的应用所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。