Related methods of regular expression in front end

Time:2021-8-12

str.match(regexp)

The method str.search (regexp) returns the location of the first match, or – 1 if it is not found

 var result = 'javascript'.search(/script/)
 console.log(result)   // 4

str.replace(str|regexp,str|func)

The method str.replace (str|regexp, str|func) is used to perform retrieval and replacement operations. If the modifier G is set in the regular expression, all substrings matching the pattern in the source string will be replaced with the string specified by the second parameter; Without the modifier g, only the first substring that matches is replaced.

console.log('12-34-56'.replace(/-/, ':'))  // 12:34-56
console.log('12-34-56'.replace(/-/g, ':')) // 12:34:56

str.split(regexp|substr,limit)

Use regular expressions (or substrings) as delimiters to split strings

console.log('12-34-56'.split(/-/))  // ["12", "34", "56"]

str.match(regexp)

Find the character matching regexp in the string STR, which has three modes:
(1) If regexp is not marked with G, it returns the first match as an array, including grouping and attributes index (the location of the match), input (input string, equal to STR)

let str = 'I love JavaScript'
let result  = str.match(/Java(Script)/)
console.log(result)  // ["JavaScript", "Script", index: 7, input: "I love JavaScript", groups: undefined]

(2) If regexp is marked with G, it returns an array of all matches as a string without grouping and other details.

let str = 'I love JavaScript'
let result  = str.match(/Java(Script)/g)
console.log(result)  // ["JavaScript"]

(3) If there is no match, null is returned regardless of whether it is marked g or not.

The regexp object defines two methods for performing pattern matching

regexp.test(str)

Its parameter is a string, which returns true if it contains a matching result of the regular expression

let str = 'I love JavaScript'
console.log(/love/i.test(str))  // true

regexp.exec(str)

Similar to the string. Match () method described earlier, its parameter is a string. A regular expression behaves differently depending on whether it carries the flag G.
If there is no G, the first match returned by regexp.exec (STR) is exactly the same as str.match (regexp);
If there is a tag g, calling regexp.exec (STR) returns the first match and saves the following location in the property regexp.lastindex. The next same call will start the search from the location regexp.lastindex, return the next match, and save the subsequent location in regexp.lastindex. This special behavior allows us to call exec () repeatedly while matching strings with regular expressions.

 var pattern = /Java/g
 var text = 'JavaScript is more fun than Java!'
 var result;
 while((result = pattern.exec(text)) != null){
     console.log("Matched'"+ result[0] + "'"+ " at position " + result.index + "; next search begins at " + pattern.lastIndex)
 }
// Matched'Java' at position 0; next search begins at 4
// Matched'Java' at position 28; next search begins at 32