History of Regular Expression
- concept developed in 1950 by mathematician Stephen Keene
- Became regularly used by Unix text processing utilities.
- Many different variations became standardized by the POSIX standard
- A version of regular expressions was used in Perl in the 1980s.
- In 1997 Philip Hazel developed PCRE for use in many modern tools
create a RegEx in JS
let regex1 = new RegExp(’hello’)
let regex2 = /world/
About Methods
RegExp Methods
- test: returns true if pattern is found in the passsed string; false if not
- exec: returns an array of matches
- toString: returns a string of the regular expressions syntax
String Methods
- match: returns an array of matches just like exec on RegExp.
- search: returns an index of the matched string
- replace: replaces matches with string
- split: splits a string into an array. The division is based on the regular expression pattern
Regular Expression Flags
/pattern/flags
or new RegExp(”pattern”, “flags”)
g
match more than one occurancei
insensitive matchm
multi-line match
wildcard .
It represents any single characterescaped character \
It mathces a “.” characterhyphen -
like 0-9 or a-zsquare brace []
range+
Matches one or more occurrences?
Matches zero or one occurrences*
. Matches zero or more occurrences{min,}
{min, max}
{min}
\b
Word boundary-Pattern bounded by a non-word character\B
Nonword boundary-Pattern bounded by a word character
Character Set Shorthand
\d [0-9]
\D [^0-9]
\w [a-zA-Z0-9_]
\W [^a-zA-Z0-9_]
\s [\t\r\n]
\S [^\t\r\n]
Groupback reference
- non capture group 使用
?:
比如 (?:\w+)
unicode character