some commonly used methods
exec
A RegExp method that performs a lookup match in a string, returning an array (null if no match is found)
var reg1= /^\w+$/ var str = 'w111fafdd' console.log(reg1.exec(str)) // ["w111fafdd", index: 0, input: "w111fafdd", groups: undefined]
It is a bit similar to the result of match, but when match matches more than one, it can return an array of all strings that meet the matching rules
The first parameter is to return a string that can match the regular rule, then the non-special characters of the case
The value of the input attribute is the original string, ie str
The index of the result indicates which string is matched from
test
A RegExp method that tests for a match in a string, returning true or false
Then the above code
console.log(reg1.test(str)) // true
search
A String method that tests for a match in a string, returning the index of the position where the match was found, or -1 on failure.
Then the above code
console.log(str.search(reg1)) // 0
match
A String method that performs a lookup match in a string, returning an array or null if no match is found.
console.log(str.match(reg1)) //["w111fafdd", index: 0, input: "w111fafdd", groups: undefined]
It is different from exec above except for the object and parameters of the call, the result is the same. When multiple strings can match the regular expression rules, the return of match is different.
Case: Withdrawing wages
Demand: var str = 'Zhang San: 1000, Li Si: 5000, Wang Wu: 8000. '; Extract the salaries of the three of them and return an array
Using the method of extracting groups
var str = 'Zhang San: 1000, Li Si: 5000, Wang Wu: 8000. '; // Write a regular expression that matches them var reg = /(\d+)/g // use an array to hold them var arr = [] while(reg.test(str)){ arr.push(RegExp.$1) //$1 is the meaning of extracting the first group, if the parentheses of the above regular expression are not added, the extraction is an empty string because there is no grouping } console.log(arr) //["1000", "5000", "8000"]
We can use a match to get it done
var str = 'Zhang San: 1000, Li Si: 5000, Wang Wu: 8000. '; var reg = /\d+/g console.log(str.match(reg))// ["1000", "5000", "8000"]
replace
A String method that performs a find match in a string and replaces the matched substring with a replacement string
str.replace(parame1,parame2); is to replace parame1 with parame2
strip all spaces from the string
The trim() method can only remove the spaces before and after. When you want to remove all the spaces in the string, use replace to replace all the spaces.
var str = " 123AD asadf asadfasf adf " var str1 = str.replace(/\s+/g,'') console.log(str1) // 123ADasadfasadfasfadf
split
A String method that separates a string using a regular expression or a fixed string, and stores the separated substrings in an array
When we used split() before, most of the strings were cut into arrays, and the brackets followed by a string
var dateStr = '2015-1-5'; var arr = dateStr.split('-') console.log(arr) // ["2015", "1", "5"]
The parameter is a regular expression
Then the above code
var arr1 = dateStr.split(/[-]/) console.log(arr1)
Quantifier: +
For example: \d: represents once \d\d: represents 2 times \d+: represents several occurrences
Global match: g
Example: /a/g
Any character: [abc];
Example: [abc]s———as,bs,cs
range[az],[0-9]
Example: id[0-9]———id0, id1, id2...id9
Exclude: [^a] exclude a character
Example: o[^0-9]———oat,o?t,ot
combination
[a-z0-9A-Z] lowercase letters or numbers or uppercase letters can only appear once
escape character
.(dot) any character
\d digits 0-9
\w az lowercase letters English, numbers, underscore
\s whitespace character
\D [^0-9]
\W [^a-z0-9]
\S non-blank
Common quantifiers
{n} occurs exactly n times
Example: \d{8} number appears exactly 8 times
[1-9]\d{7} The numbers beginning with 1 to 9 appear exactly 8 times
{nm} at least n times at most m times
Example: [1-9]\d{4,10} 5,10
{n,} minimum n times, maximum unlimited
+ {1,} at least 1 occurrence at most unlimited
? {0,1} at least 0 times at most 1 times optional
Example: 010-87495698-23 (0\d{2,3})?[1-9]\d{7}(-\d{1,5}) Partition optional 8-digit extension 1 is or 5 digits
* {0,} can be either None or Wireless