1. Methods to define regular expressions
There are two ways to define regular expressions: constructor definition and regular expression direct quantity definition. For example:
Copy the code as follows: var reg1 = new RegExp('/d{5, 11}'); // Define through the constructor
var reg2 = //d{5, 12}/; // Definition by direct quantity
Regular expressions directly measure characters
/o: NUL characters (/u0000)
/t: Tab character (/u0009)
/n: newline character (/u000A)
/v: Vertical tab character (/u000B)
/f: Page change (/u000C)
/r: carriage return character (/u000D)
/xnn: Latin character specified by the hexadecimal number nn, for example, /x0A is equivalent to /n
/uxxxx: Unicode characters specified by the hexadecimal number xxxx, for example /u0009 is equivalent to /t
/cX: Control character ^X, for example, /cJ is equivalent to line break/n
Regular expression anchor characters
^: Match the beginning of a string, in multi-line search, match the beginning of a line
$: Match the end of a string, in multi-line search, match the end of a line
/b: Match the boundary of a word. In short, it is located between characters /w and /W, or between the beginning or end of character /w and string ([/b] matches it Backspace symbol)
/B: Match the position of non-word boundaries
(?=p): Zero-width forward assertion is required to match p, but not to include those characters that match p
(?!p): Zero-width negative direction asserts first, requiring that the next string does not match p
Character classes for regular expressions
[...]: Any character in square brackets
[^...]: any character not in square brackets
.: Any character except line breaks and other Unicode line terminators
/w: Any word composed of ASCII characters, equivalent to [a-zA-Z0-9]
/W: Any word that is not composed of ASCII characters is equivalent to [^a-zA-Z0-9]
/s: Any Unicode whitespace
/S: Any non-Unicode whitespace characters, note that /w and /S are different
/d: Any ASCII number, equivalent to [0-9]
/D: Any character except ASCII number, equivalent to [^0-9]
[/b]: Backspace direct quantity (special case)
Repeat character syntax for regular expressions
{n, m}: Match the previous item at least n times, but not more than m times
{n, }: Match the previous item n times or more times
{n}: Match the previous item n times
?: Match the previous item 0 or 1 time, that is, the previous item is optional, equivalent to {0, 1}
+: Match the previous item 1 or more times, equivalent to {1, }
*: Match the previous item 0 or more times, equivalent to {0, }
Selection, grouping and reference characters of regular expressions
|: Select, matching the subexpression on the left or subexpression on the right
(…): Combination, combine several items into a unit, which can be modified by symbols such as "*", "+", "?" and "|", and can remember the characters matching this group. String for any subsequent use
(?: ...): Only combine items into a unit, but do not remember characters matching the shuffle
/n: matches the character that matches the first time the nth group matches. The group is a subexpression in parentheses (which may also be nested). The group index is a number of left brackets from left to right. "(? :" form grouping is not encoded
Regular expression modifier
i: Perform case-insensitive matching
g: Perform a global match, in short, find all matches, instead of stopping after finding the first one
m: Multi-line matching pattern, ^ matches the beginning of a line and the beginning of a string, $ matches the end of a line and the end of a string
String method for pattern matching
search(): Its argument is a regular expression, returning the starting position of the first substring to match it, and returns -1 if there is no matching substring. If the parameter of search() is not a regular expression, it will first be converted to a regular expression through the RegExp constructor, search() does not support global search because it ignores modifier g. like:
The code copy is as follows:
var s = "JavaScript".search(/script/i); // s = 4
replace(): It is used to perform retrieval and replacement. Receive two parameters, the first is the regular expression, and the second is the string to be replaced. If the regular expression is set, a global substitution is performed, otherwise only the first substring of the matching is replaced. If the first argument is not a regular expression, search for the string directly instead of converting it to a regular expression. like:
Copy the code as follows: var s = "JavaScript".replace(/java/gi, "Script"); // s = Script Script
match(): Its argument is a regular expression. If not, it is converted through RegExp, and it returns an array of matching results. If modifier g is set, global matching is performed. like:
Copy the code as follows: var d = '55 ff 33 hh 77 tt'.match(//d+/g); // d = ["55", "33", "77"]
split(): This method is used to split the string that calls it into an array composed of substrings. The delimiter used is the parameter of split(), and its parameters can also make a regular expression. like:
Copy the code as follows: var d = '123,31,453,645'.split(','); // d = ["123", "31", "453", "645"]
var d = '21 , 123, 44, 64, 67, 3'.split(//s*,/s*/); // d = ["21", "123", "44", "64" , "67", "3"]
2. RegExp object
Each RegExp object has 5 properties. The property source is a read-only string containing text of a regular expression. The property global is a read-only boolean value to indicate whether the regular expression has a modifier g. The property ignoreCase is a read-only boolean value to indicate whether the regular expression has a modifier i. The property multiline is a read-only boolean value to indicate whether the regular expression has a modifier m. The property lastIndex is a readable and writable integer. If the matching pattern has a g modifier, this property stores the start position of the next search in the entire string.
There are two methods for the RegExp object. The exec() parameter is a string, and its function is similar to match(). The exec() method executes a regular expression on a specified string, that is, performs matching search in a string. If no match is found, return null. If a match is found, return an array. The first element of this array contains a string that matches the regular expression, and the remaining elements are equivalent to the subexpression in parentheses. The matching substring, regardless of whether the regular expression has modifier g, will return the same array. When the regular expression object calling exec() has a modifier g, it sets the lastIndex property of the current regular expression object to the character position next to the matching substring. When the same regular expression calls exec() the second time, it will start to retrieve from the string indicated by the lastIndex property, and if exec() finds no matching results, it resets lastIndex to 0. like:
The code copy is as follows: var p = /Java/g;
var text = "JavaScript is more fun than Java!"
var r;
while((r = p.exec(text)) != null) {
console.log(r, 'lastIndex: ' + p.lastIndex);
}
Another method is test(). Its argument is a string. Use test() to check a certain string. If it contains a matching result of the regular expression, it will return true otherwise false. like:
The code copy is as follows: var p = /java/i;
p.test('javascript'); // true