当前位置:主页 > 查看内容

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

发布时间:2021-07-02 00:00| 位朋友查看

简介:主要使用的有下面三种: 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 ite……

主要使用的有下面三种:

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)


原文链接:https://m.jb51.net/article/21241.htm
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:JS 正则表达式的位置匹配 下一篇:没有了

推荐图文


随机推荐