欢迎来到代码驿站!

当前位置:首页 >

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

时间:2021-02-10 14:49:43|栏目:|点击:

主要使用的有下面三种:

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)

上一篇:当ES6遇上字符串和正则表达式

栏    目:

下一篇:Powershell小技巧之通过EventLog查看近期电脑开机和关机时间

本文标题:JS 正则表达式中小括号的应用

本文地址:http://www.codeinn.net/misctech/60342.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有