1. The regular expression test method
The test () method is used to detect whether a string matches a pattern
return value:
Returns trueif the string contains text that matches Regexpobject, otherwise false.
2. The regular expression Exec method
The exec () method is used to retrieve the matching of regular expressions in a string.
Return value: returns an array that holds the results of the match. If no match is found, the return value is null.
3. String Search method
The search () method is used to retrieve the substring specified in a string, or to retrieve a substring that matches a regular expression.
Return value: The starting position of the first substring in stringobject that matches the regexp.
4. Sample code:
<!DOCTYPE HTML><HTMLLang= "zh"> <Head> <MetaCharSet= "UTF-8" /> <title>Match and replace methods for strings in JavaScript</title> </Head> <Body> <Scripttype= "Text/javascript"> varStr= "Visit W3school w3school!" //Console Output 6Console.log (Str.search (/W3school/)); varReg= NewRegExp ("W3school"); //Console Output True indicates if the string contains text that matches the regular expressionConsole.log (Reg.test (str)); //Console output: "W3school", Index:6, Input: "Visit w3school w3school!" //The first "W3school" represents the text that matches the regular expression //The second index:6 represents the position of the first character of the matched text //Third input: "Vvisit w3school w3school!" A string that indicates that the retrieved strings are storedConsole.log (reg.exec (str)); </Script> </Body></HTML>
Note: The exec usage of regular expressions is more complex, the test method of regular expressions and the search method of strings are relatively concise .
The difference between the JS regular expression test method, the Exec method and the string search method