How to use
1. Create an expression
Methods to create regular expression classes in JavaScript:
var regex = new RegExp("//d{5}") or 2.var regex = / /d{5} / (recommended)
/Expression/ is a syntax specially provided in JavaScript to simplify the writing of regular expressions. Regular expressions written in // do not need to worry about escape characters.
Methods of RegExp objects:
2. Determine whether it matches
test(str) determines whether the string str matches a regular expression, which is equivalent to IsMatch
The code copy is as follows:
var regex = /.+@.+/;
alert(regex.test("[email protected]"));
alert(regex.test("ab.com"));
3. Get matching results
Exec(str) searches for matches, and the return value is the match result (*), which is equivalent to match() and matches() in C#
If exec() finds matching text, a result array is returned (exactly matched strings and the result of extracting the group.). Otherwise, return null. To extract multiple methods, you need to call exec() repeatedly similar to matches() method.
Pay attention to global mode/…../g
In non-global mode, calling exec() once is equivalent to match();
In global mode, multiple consecutive calls are equivalent to matches()
---i Ignore upper and lower case
---mMultiple line matching
The above content is an introduction to the use of regular expressions in JavaScript in this article. I hope you like it.