JS 正则表达式中小括号的应用
2019/6/29 21:04:54
本文主要是介绍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.
(?=p) A 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 正则表达式中小括号的应用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-11正则表达式学习:入门指南与实践技巧
- 2024-08-15正则表达式入门:基础教程与实践指南
- 2024-01-0939. 干货系列从零用Rust编写负载均衡及代理,正则及格式替换
- 2024-01-08如何编写高效的正则表达式?
- 2023-12-29"Matlab中的正则表达式:强大而灵活的工具"
- 2023-09-30这个正则 为啥同样的单号第二个就提取不出来?
- 2023-06-086.2 re 正则表达式
- 2023-06-06将字符串里的\x01,\x02这些替换掉用正则表达式无效?
- 2023-05-24正则表达式详解
- 2023-05-17我让gpt写了一段正则表达式代码,可是运行报错,可以帮忙看看哪里出了问题?