split()
- You can split a string into an array
- The method can pass a regular expression as an argument, and the method will split the string based on the regular expression
- This method splits everything even if it doesn’t specify a global match
var str = "1a2b3c4d5e6f7";
var result = str.split(/[A-z]/);
console.log(result); / / "1,2,3,4,5,6,7"
Copy the code
search()
- You can search for the specified content in a string
- The first occurrence of the index subscript is returned if the specified content is found, or -1 if not
- It can take a regular expression as an argument and then retrieve the string based on the regular expression
- Search () can only find the first one, even if the global mode is configured
Searches for ABC or AEC or AFC in the string and returns the first occurrence of the index subscript if so
var str = "hello abc world aec afc";
var result = str.search(/a[bef]c/);
console.log(result); / / "6"
Copy the code
match()
- You can extract qualified content from a string based on a regular expression
- By default, our match will only find the first content that matches the requirements, and then stop searching
- We can set the regular expression to global match mode (G), which will match everything
- Multiple matching patterns can be set for a regular expression in any order
- Match () encapsulates the matched content into an array and returns it, even if only one result is found
var str = "1a2b3c4d5e6f7A8B9";
var result = str.match(/[a-z]/ig);
console.log(result); //"a,b,c,d,e,f,A,B"
console.log(typeof result); //"object"
console.log(Array.isArray(result)); //"true"
console.log(result[2]); //"c"
Copy the code
replace( )
- You can replace the specified content in a string with the new content
Parameters:
- 1. The content to be replaced can take a regular expression as an argument
- 2. New content
- By default, only the first one is replaced
var str = "1a2a3a4d5e6f7A8B9";
var result = str.replace(/[a-z]/gi.# _ "&"); / / "1 # _ # _ # _ & 2 & 3 & 4 # _ # _ & 5 & 6 # _ # _ # _ & 7 & 8 & 9"
console.log(result);
// Can also play delete function
result = str.replace(/[a-z]/gi.""); / / "123456789"
console.log(result);
Copy the code
quantifiers
- Quantifiers allow you to set the number of occurrences of a content
- A quantifier only works on the content that precedes it
- {n} occurs exactly n times
- {m,n} occurs m-n times
- {m,} occurs more than m times
- + at least once, equivalent to {1,}
- * 0 or more times, equivalent to {0,}
- ? 0 or 1, equivalent to {0,1}
var reg = /a{3}/;
console.log(reg.test("aaaabca")); //true
reg = /(ab){3}/;
console.log(reg.test("abababsdsab")); //true
reg = {1, 3} / / (ab);
console.log(reg.test("ababsdsab")); //true
reg = /ab{1,}c/;
console.log(reg.test("abbbbbc")); //true
reg = /ab+c/;
console.log(reg.test("abbbbbc")); //true
reg = /ab*c/;
console.log(reg.test("ac")); //true
reg = /ab? c/;
console.log(reg.test("ac")); //true
Copy the code
- ^ indicates the beginning
- $indicates the end
var reg = /^a/;
console.log(reg.test("ac")); //true
reg = /a$/;
console.log(reg.test("ca")); //true
Copy the code
There can only be one A
reg = /^a$/;
console.log(reg.test("a")); //true
Copy the code
Begin or end with an A
reg = /^a|a$/;
console.log(reg.test("ca")); //true
Copy the code
- . Finds any single character except newline and line terminator
- When special symbols are displayed, the escape character \ is required
var reg = / /. /;
console.log(reg.test("a.")); //true
reg = / \ \ /;
console.log("a\\"); //"a\"
console.log(reg.test("a\\")); //true
Copy the code
- \w Any alphanumeric _
- \W except alphanumeric _
var reg = /\w/;
console.log(reg.test("_")); //true
reg = /\W/;
console.log(reg.test("%")); //true
Copy the code
- \d Any number
- \D Except for any number
var reg = /\d/;
console.log(reg.test("123")); //true
reg = /\D/;
console.log(reg.test("%dr")); //true
Copy the code
- \ s Spaces
- \S Except Spaces
var reg = /\s/;
console.log(reg.test(23 "1")); //true
reg = /\S/;
console.log(reg.test("%dr")); //true
Copy the code
- \b Word boundary
- \D Except for word boundaries
var reg = /\bchild\b/; // Find the word child
console.log(reg.test("hello children")); //"false"
console.log(reg.test("hello child")); //true
reg = /\Dchild\D/;
console.log(reg.test("hellochildren")); //true
Copy the code