Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

正则表达式中的 "?:" 的作用 #17

Open
ScoutYin opened this issue Jan 10, 2019 · 0 comments
Open

正则表达式中的 "?:" 的作用 #17

ScoutYin opened this issue Jan 10, 2019 · 0 comments

Comments

@ScoutYin
Copy link
Owner

ScoutYin commented Jan 10, 2019

(?:x) 匹配 'x' 但是不记住匹配项,因此也不能反向引用该匹配值,这种叫作非捕获括号

const reg1 = /(\w+),(\d+),(\w+)/
const reg2 = /(?:\w+),(?:\d+),(\w+)/
const str = 'abc,123,de'

str.match(reg1)
// ["abc,123,de", "abc", "123", "de"]
str.replace(reg1, '$1哈哈$2哈哈$3') // $n (n是一个数字,表示第n个捕获组的内容)
// "abc哈哈123哈哈de"

str.match(reg2)
// ["abc,123,de",  "de"]
str.replace(reg1, '$1哈哈$2哈哈$3')
// "de哈哈$2哈哈$3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant