A regular expression is a literal pattern consisting of ordinary characters (such as characters A through Z) and special characters (called metacharacters). A regular expression acts as a template to match a character pattern to the searched string. Regular expression = Normal characters + Special characters (metacharacters)
A, grammar,
/ Regular expression body/modifiers (optional) Examples:var patt = /runoob/i
Copy the code
Second, the use of
Define a regular expression
// The first "/ regular expression/"
var reg1=/ hello \ w/g {3, 12};
// New RegExp(' regular expression ')
var reg2=new RegExp(Three "hello \ \ w {12}".'g');
/** * note that in the second method, "\\" stands for "\" due to string escape problems. * /
Copy the code
RegExp constructor
The first argument is the text content of the regular expression
The second parameter is the optional flag. Flags can be used in combination
- G (Full-text Search)
- I (ignore case)
- M (multi-line lookup)
var re = new RegExp("a"."gi");// Matches all a or A
var re = /a/gi;
Copy the code
Note:
-
Both arguments passed to the RegExp constructor are strings
-
Regular expression literals cannot be passed to the RegExp constructor
-
Because the RegExp constructor’s schema argument is a string, to double-escape characters, all metacharacters must be double-escaped, as must those escaped characters. (For example, the n character is escaped as \ in a string, but becomes \ in a regular expression string)
Literal pattern | Corresponding string pattern |
---|---|
/.at/ | “\.at” |
/ \ d \ d {1, 2} / | “\ d \ d {1, 2}” |
/\w\hello\123/ | “\w\\hello\\123” |
Three, common methods
test()
Used to test whether a string matches a pattern
Returns true if the string contains matching text, false otherwise.
var str = 'Sumi122Sumi';
var re1 = /su/; // Can the string match 'su'
var re2 = /ui/; // Can the string match 'ul'
var re3 = /\d/; // Whether a number can be matched in the string
console.log(re1.test(str)); //false Because the re is case sensitive, there is no 'su', so return false
console.log(re2.test(str)); //false
console.log(re3.test(str)); //true
Copy the code
search()
Finds the first occurrence of the matching string (case sensitive)
- If found, the subscript value for that location is returned
- If none is found, -1 is returned
var str = 'Sumi122Sumi';
var re1 = /Su/; // find the location where Su first appears
var re2 = /ui/; // find the location where the UI first appears
var re3 = /\d/; // Find the position where the number first appears
// The argument is regular
console.log(str.search(re1)); / / 0
console.log(str.search(re2)); / / 1
console.log(str.search(re3)); / / 4
// The argument is a string
console.log(str.search('su')); // the -1 search() method is case sensitive, so su is not available here
console.log(str.search('Su')); / / 0
console.log(str.search('ui')); / / 1
Copy the code
match()
Find the matching string (case sensitive), store it in the array, and return the array
Return value array
- Without the G modifier, only one result is matched and index and input attributes are added to the array found
- Index: indicates the subscript value of the matching result
- Input: The original character string
-
If there is a g modifier, it is a global match, and all matching characters found are in the result
-
If not found, null is returned
var str = 'sumi12su122mi1222su123mi';
var re1 = /u/;
var re2 = /\d/;
var re3 = /i/g;
var re4 = /\d/g;
var re5 = /\d\d/g;
var re6 = /\d+/g;
var re7 = /a/;
// Since there is no G modifier, only one result is matched, and the index and input attributes are added
console.log(str.match(re1)); //["u", index: 1, input: "sumi12su122mi122su123mi"]
console.log(str.match(re2)); //["1", index: 4, input: "sumi12su122mi122su123mi"]
// Since there is a g modifier, all results will be matched. No index and input attributes will be added
console.log(str.match(re3)); //["i", "i", "i"]
console.log(str.match(re4)); / / / "1", "2", "1", "2", "2", "2", "1", "2", "2", "1", "2", "3"]
// Enter two consecutive \d to find all two connected digits
console.log(str.match(re5)); //["12", "12", "22", "12", "12"]
// If a number appears at least once, the number of characters to be found is not fixed, i.e., all connected digits are found together and stored in the array
console.log(str.match(re6)); / / / "12", "1222", "122", "123"]
// Not found, return null
console.log(str.match(re7)); //null
Copy the code
replace()
Replace the first matched string (case sensitive) (original string unchanged)
parameter
First argument: the string to match
The second argument: replaces the content of the matched string
If the second argument is a function
1. The value must be returned; otherwise, undefined will be used to replace the replaced content
2. The function takes three arguments:
First argument: the string to match
Second argument: the subscript value of the matched string
The third argument: the original string
var str = 'sumisumi';
// The first argument can be a string or a re, and only the first matching string is replaced
var newStr1 = str.replace('s'.'l');
console.log(newStr1,str); //lumisumi sumisumi
var newStr2 = str.replace(/s/.'l');
console.log(newStr2,str); //lumisumi sumisumi
// If there is a g modifier, then all matches can be replaced
var newStr3 = str.replace(/s/g.'1');
console.log(newStr3); //1umi1umi
// If the replace method's second argument is a function, it must return a value. Otherwise, replace the content with undefined
var newStr4 = str.replace(/s/.function(){
return 'l';
});
console.log(newStr4); //lumisumi
var newStr4 = str.replace(/s/.function(){});
console.log(newStr4); //undefinedumisumi
// The function takes three parameters: the string to be matched, the subscript value of the matched string, and the original string
var newStr5 = str.replace(/s/.function(a,b,c){
console.log(a,b,c); //s 0 sumisumi
});
Copy the code
split()
Splits a string into an array of strings.
var reg=8 / / [1, 2, 3];
console.log('hello128Javascript138Javascript178Javascript'.split(reg));
//['hello','Javascript','Javascript178Javascript']
Copy the code
4. Basic rules
First, the matching character
In regular expressions, the contents enclosed in square brackets are called “character clusters”, which represent a range, but in practice, only a fixed character can be matched.
[A-z] : Matches lowercase letters from any character in a-z
[A-z] : matches uppercase letters from any character in a-z
[0-9] : matches any character from 0-9, equivalent to \d
[0-9A-z] : matches digits from 0-9 or lowercase letters from A-Z
[0-9A-zA-z] : matches any character of digits from 0-9, lowercase letters from A-Z, or uppercase letters from A-Z
[abcd] : matches any character in the character ABcd
[1234] : Matches any character in the number 1234
[U4e00-U9FA5] : U4e00-U9FA5
^ (debyte)
There is a special symbol ^ (debyte) in the character cluster. Debyte in the character cluster means invert
[^a-z] : matches any character except lowercase letters from a-z
[^0-9] : matches any character except digits from 0-9
[^abcd] : matches any character except abcd
There is another class of special characters in regular expressions, as follows:
\ D: Matches a numeric character, represented by a character cluster [0-9]
\D: Matches a non-numeric character, which can also be represented by a character cluster [^0-9] or [^\ D]
\ W: Matches any word character including underscore with character cluster [0-9a-za-z_]
\W: Matches any non-word character, [^ 0-9a-za-z_] or [^\ W]
\s: Matches any whitespace character. Spaces, tabs, and newlines can all be matched by \s
\S: Matches any non-whitespace character, [^\ S]
. (I’m a dot) : matches any single character except “\n” (newline character)
var str ='abc defg';
var reg= /[bcde]/gi;
var reg1 = /@/gi;
var reg2 = /\w/gi;
var reg3 = /\W/gi;
var reg4 = /[^\s]/gi;
console.log(str.match(reg)) // ["b","c","d","e"]
console.log(str.match(reg1)) // null
console.log(str.match(reg2)) // ["a", "b", "c", "d", "e", "f", "g"]
console.log(str.match(reg3)) // [" ", ""," "]
console.log(str.match(reg4)) // ["a", "b", "c", "d", "e", "f", "g"]
Copy the code
2. Qualifiers
The qualifier specifies how many times a given component of a regular expression must occur to satisfy a match.
* Matches the preceding subexpression zero or more times, from 0 to more, using {0,} instead
+ matches the preceding subexpression one or more times, 1 to more, using {1,} instead
? Matches the preceding subexpression zero or once, 0 or 1, and can be replaced with {0,1}
{n} matches a certain n times, such as {18}, 18 consecutive matches
{n,} matches at least n times. For example, {1,} indicates that the match is at least once
{n,m} matches at least n times and at most m times. For example, {1,7} matches at least 1 times and at most 7 times
var str = 'abc12345de4567ff456789g';
var reg = /\d{4}/g
var reg1 = /\d{3,}/g
console.log(str.match(reg)) / / / "1234", "4567", "4567"]
console.log(str.match(reg1)) / / / "12345", "4567", "456789"]
Copy the code
Greed matching principle
In regular expressions:
We call this situation of more matches than less matches the greedy matching principle, which is used by default in regular expressions.
We can also add a question mark (?) after the qualifier in regular expressions. To change the matching principle to the “non-greedy matching principle”, can match less will not match more.
var str = 'abc12345de4567ff456789g';
var reg = / \ d {3, 5}? /g
console.log(str.match(reg)) // ["123", "456", "456", "789"]
Copy the code
Three, locator
A locator can fix a regular expression at the beginning or end of a line. You can also create regular expressions that appear only within a word or at the beginning or end of a word.
^ Matches the beginning of the input string (starting with ***)
$matches the end of the input string (ending with ***)
\b Matches a word boundary (string beginning, end, space, comma, period, etc.)
\B Matches non-word boundaries
var str = '156521 587'
var reg = /1[345678]\d{9}$/g
if(reg.test(str)){
console.log("Reasonable phone number.")}else{
console.log("Wrong phone number.")}Copy the code
4. Escape characters
In regular expressions, we must use the escape character (backslash \) to escape special symbols.
() [] * +? (I am dot) / \ ^ $
var str = '! @ # $% ^ () [] ^ ^ ^ ';
var reg =/\^/g;
console.log(str.match(reg)) // ["^", "^", "^", "^", "^"]
Copy the code
5. Select the matching character
In the regular expression, if you want to match the content contains multiple choice, we can choose by | a match to match
var str = 'helloJavaScript, hellojquery'
var reg = /hello(JavaScript|jquery)/gi
console.log(str.match(reg)) // ["helloJavaScript", "hellojquery"]
Copy the code
Six, special usage
(? =) : indicates the forward check
Matching results? = Matching condition. The matching result can be output only when the matching condition is met
(? !). : Negative pre-check
Matching results? ! Matching condition: the matching result can be output only when the matching condition is not met
(? : : Outputs content but does not capture it
var str = 'helloJavaScript, hellojquery'
var reg = /hello(? =JavaScript)/gi;
var reg1 = /hello(? =css)/gi;
var reg2 = /hello(? ! JavaScript)/g
var reg3 = /hello(? :JavaScript|jquery)/gi
console.log(str.match(reg)) // ["hello"]
console.log(str.match(reg1)) //null
console.log(str.match(reg2)) // ["hello"]
console.log(str.match(reg3)) // ["helloJavaScript", "hellojquery"]
Copy the code