This article mainly introduces the use of regular expressions in JavaScript, very detailed, with a simple example, recommended here.
How to use
1. Create an expression
Methods for creating regular expression classes in javascript:
var regex = new RegExp ("d{5}") or 2.var regex =/d{5}/(Recommended)
/Expression/is the syntax provided in JavaScript specifically to simplify regular expression writing, and regular expressions written in//are not used for escape characters.
Ways to RegExp objects:
2. Judge whether the match
Test (str) determines whether string str matches a regular expression, equivalent to IsMatch
The code is as follows:
var regex = /.+@.+/;
Alert (Regex.test ("a@b.com"));
Alert (Regex.test ("ab.com"));
3. Get matching Results
EXEC (str) searches for a match with the return value of the matching result (*), equivalent to match () and matches () in C #
If EXEC () finds a matching text, it returns an array of results (the exact matching string and the result of the extraction group). )。 Otherwise, NULL is returned. To extract multiple calls to Exec () is similar to the matches () method.
Note Global Mode/...../g
In non-global mode, calling exec () is equivalent to match ();
Repeated calls in global mode are equivalent to matches ()
---i ignore case
---m multiple-line match
The above content is this article about the use of JavaScript in the expression of the introduction, I hope you can enjoy.