The definition in JavaScript1. A regular expression is defined by the RegExp constructor function. The first parameter is a regular expression that is passed in as a string, and the second parameter is a modifier that also passes in as a string.
例:var caps = new RegExp("[A-Z]", "g");
2, directly define a regular expression direct amount, the expression right forward slash symbol "/" separated, followed by the modifier. (Recommended for this)
例:var caps = /[A-Z]/g;
characters commonly used in JavaScript regular expressions
character |
Description |
Example |
[Exp] |
For a sequence of characters wrapped in brackets ([]), the regular expression handler matches any one of the characters within the brackets . |
[Exp] matches any one of the characters in the E,x,p |
[^exp] |
Precede the brackets with the ^ character, which will match any symbol outside the brackets, except the meaning . |
[^exp] matches any character except E, X, p |
[EXP1-EXP2] |
Use a hyphen to represent any character in the EXP2 character sequence that matches the EXP1 character. |
[A-z] matches a-Z any one character; [0-9] matches 0-9 any number |
(exp) |
For a sequence of characters wrapped in parentheses, the regular expression is matched exactly in that character order (contains can be matched). |
(exp) matches the string "exp" |
(exp1\ |
EXP2) |
Use pipe characters \ |
exp+ |
The expression is followed by A + character, which indicates that the expression is matched only if it is included one or more times . |
exp+ Matching EXP |
\s |
Match whitespace characters, that is, Spaces, tab tabs, carriage returns |
A\SB match string with a white space character B |
\s |
Matches any one character except for whitespace characters unexpectedly |
A\SB match A is not a white space character any characters b |
\d |
Match a number from 0-9 |
|
\d |
Match any one character except the number |
|
\w |
Match a literal character, that is, a word, a number, or a letter |
|
\w |
Matches any character except for a literal character unexpectedly |
|
In addition to the commonly used expressions, there are modifiers that define how regular expressions are used, there are three possible values that can be used individually as options, and can be used more than once.
modifier |
Description |
G |
Apply regular expressions to find all matching items in the comparison string, instead of returning only the first occurrence |
I |
Ignore case when applying an expression to compare matches |
M |
Apply an expression to match multiple lines of text, not just the first line |
Common uses of JavaScript regular expressions
There are three types of string methods that can use regular expressions. Match () finds all the sub-characters that can match the regular expression and returns the result as an array of strings. The replace () method looks for the same child character as the former and replaces it with another string passed in to the method. Search () Simply locates the position of the first substring that matches the regular expression, and returns the position of the string in the entire string as a numeric ordinal.
var regEx = /他妈的/g, string = "我去你他妈的"; alert(string.match(regEx)); //他妈的 alert(string.search(regEx)); //3 alert(string.replace(regEx, "*")); //我去你*
replace () Special usage
A special character for the second parameter of the replace () method that can be used as a JavaScript string
character Sequences |
meaning |
Example |
$$ |
Replace the found string with a separate $ character |
"Hello World". Repl Ace (/o/g, "\$\$");//"hell\$ w\ $rld" |
$& |
use A string given in one parameter to replace the found substring |
"Hello World". Replace (/o/g, "$&");//"Hello World" |
$ ' (ESC below)
|
"Hello World". Replace (/o/g, "$ ' ");//" Hellhell Whello wrld " |
$ ' |
|
"Hello World". Replace (/o/g, "$");//"Hell World Wrldrld" |
\$1, \$2 |
|
"Hello World". Replace (/(O) (r)/g, "\$1\$2\$1\$2");//"Hello Wororldd" (Find or, then replace with Oror |
Another is that the second parameter of replace () can be passed in as a function, and then replace the atomic string with the return value of the function.
var count = 0;function replaceWithCount() { count = count + 1; return count; }alert("Hello World".replace(/o/g, replaceWithCount)); //Hell1 W2rldalert("Hello World".replace(/\s/g, replaceWithCount)); //Hello3World
Summary
This is just the regular expression I wrote as a note some simple judgments and JavaScript usage. Regular expressions can also be very complex and powerful, then you need to use the time to dig into the thinking. If you want to learn more, you can learn about the guidelines for using regular expressions in JavaScript on the Mozilla Developer Network. Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions written in more detail.
JavaScript Regular expression notes (reproduced)