regexp
Assetions
^
Start$
End\b
Word boundary\B
None word boundaryx(?=y)
Match x only x followed by yx(?!y)
Match x only y not followed by y(?<=y)x
Math x if x follows y(?<!y)x
Math x if x not follows y
Refer
Charactor Classes
\
Escape\cX
\xhh
\uhhhh
\u{hhhh} or \u{hhhhh}
\
Group and Rangers
(x)
捕获括号
捕获组: 匹配x并记住匹配项。例如,/(foo)/匹配并记住“foo bar”中的“foo” 。捕获组会带来性能损失。如果不需要收回匹配的子字符串,请选择非捕获括号(?:x)
string.match()
- 如果使用g标志,则将返回与完整正则表达式匹配的所有结果,但不会返回捕获组。
- 如果未使用g标志,则仅返回第一个完整匹配及其相关的捕获组(Array)可使用
matchAll
string.machAll()
RegExp必须是设置了全局模式g的形式
regxp.exec()
const regexp = RegExp('foo[a-z]*','g');
const str = 'table football, foosball';
let match;
while ((match = regexp.exec(str)) !== null) {
console.log(`Found ${match[0]} start=${match.index} end=${regexp.lastIndex}.`);
// expected output: "Found football start=6 end=14."
// expected output: "Found foosball start=16 end=24."
}
`foo bar`.match(/(foo)/) // 返回完整匹配 以及捕获组
// ['foo', 'foo', index: 0, input: 'foo bar', groups: undefined]
`foo bar`.match(/(foo)/g) // 设置了 `g` flag 不会返回捕获组
// ['foo']
`foo bar`.match(/foo/)
// ['foo', index: 0, input: 'foo bar', groups: undefined]
'test1test2'.matchAll( /t(e)(st(\d?))/g) // 必须携带 `g` flag
// ['test1', 'e', 'st1', '1', index: 0, input: 'test1test2', groups: undefined]
// ['test2', 'e', 'st2', '2', index: 5, input: 'test1test2', groups: undefined]
string.replace()
`aabbcdef`.replace(/\w/,'$$')
// '$abbcdef'
`aabbcdef`.replace(/\w/,'$&')
// 'aabbcdef'
`aabbcdef`.replace(/(\w)/,'$&')
// 'aabbcdef'
`a1abbcdef`.replace(/(\d)/,'$&')
// 'a1abbcdef'
`a1abbcdef`.replace(/(\d)/,'$&')
// 'a1abbcdef'
`a1abbcdef`.replace(/\d/,'$$')
// 'a$abbcdef'
`a1abbcdef`.replace(/\d/,'$&')
// 'a1abbcdef'
`a1abbcdef`.replace(/\d/,'$`')
// 'aaabbcdef'
`aX1abbcdef`.replace(/\d/,'$`')
// 'aXaXabbcdef'
`aX1abbcdef`.replace(/\d/,"$'")
// 'aXabbcdefabbcdef'
`aX1abbcdef`.replace(/\d/,'$1')
// 'aX$1abbcdef'
`aX1abbcdef`.replace(/(\d)/,'$1')
// 'aX1abbcdef'
`aX1abbcdef`.replace(/(\d)/,(...args) => {
console.log(args)
// ['1', '1', 2, 'aX1abbcdef']
// match p1 offset string
})
abc|def
abc
或者 def
[abc] [^abc]
字符集
\n
反向引用捕获括号
其中n是一个正整数。对正则表达式中与n括号匹配的最后一个子字符串的反向引用(计算左括号)。例如,/apple(,)\sorange\1/ 匹配 “apple,orange,cherry,peach” 中的 "apple,orange,", 其中 \1 引用了 之前使用 () 捕获的 ,
`apple,orange,cherry,peach`.match(/apple(,)orange\1/) // `\1`表示第一个捕获括号
// ['apple,orange,', ',', index: 0, input: 'apple,orange,cherry,peach', groups: undefined]
(?:x)
非捕获括号
`apple,orange,cherry,peach`.match(/apple(?:,)orange\1/)
// null
`apple,orange,cherry,peach`.match(/apple(?:,)orange/)
// ['apple,orange', index: 0, input: 'apple,orange,cherry,peach', groups: undefined]
非捕获组: 匹配 “x”,但不记得匹配。
Quantifiers
量词
默认情况下 量词用的是贪婪模式 ,量词后接 ?
表示非贪婪模式
x*?
x+?
x??
x{n}?
x{n,}?
x{n,m}?
`some <foo> <bar> new </bar> </foo> thing`.match(/<.*>/)
// ['<foo> <bar> new </bar> </foo>', index: 5, input: 'some <foo> <bar> new </bar> </foo> thing', groups: undefined]
`some <foo> <bar> new </bar> </foo> thing`.match(/<.*?>/) // 非贪婪模式
// ['<foo>', index: 5, input: 'some <foo> <bar> new </bar> </foo> thing', groups: undefined]
query解析
/([^?=&]+)(=([^&]*)
const getURLParameters = url =>
(url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce(
(a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a),
{}
);
query对象转成queryString
Object.entries(aa).reduce((acc,item) =>(acc+=`&${item[0]}=${item[1]}`,acc),'').slice(1)