Modifier (g, i, m)
-
g: full match (without the first match to the stop)
-
i: Ignore case (default case-sensitive)
-
m: multiline search
“He is a boy,Is he?”.replace('/\bis\b/gi','0')
=> "He 0 a boy,0 he?"
Metacharacters (*,, [], (), {}, +,, $, ^, |.? ,,)
- [] Character class, one can meet
[abc] 字符类,(表或,有其中一个即可(a,b,c))
[^abc] 字符类取反,none of (a,b,c) 不包含组合里任意一个
[a-z] 从a至z的任意字符
[0-9] 从0至9的任意数字
[a-zA-Z] 从a至z或A至Z的任意字符
- All symbols except the carriage return and linefeed
- \ D numeric characters ([0-9])
- \ D non-numeric characters ([^ 0-9])
- \ S whitespace
- \ S non-whitespace
- \ W word character (alphanumeric underscore [a-zA-Z_0-9])
- \ W non-word character
- {4} quantifier, expressed 4
- () Grouping
- Represents one or more + (\ + D 1 represents one or more numbers)
- ? Represents 0 or 1 (\ d? Represents 0 or a number)
2020-05-06 替换为05-06-2020
var reg = /^(\d{4})[/-](\d{2})[/-](\d{2})$/g
"2020-05-06".replace(reg,'$2-$3-$1')
Boundary (^, $, \ b, \ B)
- ^ XXX to start (pre-write)
- $ XXX to the end (after writing)
- \ b word boundary
- \ B non-word boundary