1. The match function wakes up the string with the specified regular expression function and returns the matching string prototype as an array: Stringobj.match (REGEXP) Parameter: Stringobj required option, need to go to match string regExp required option, The specified regular expression return value: If the G (Global match) option is not used, the first matched string, the location of the string, and an array of the original string are returned, and if the G option is used, an array of all matching strings is returned example 1:varstr ="AAABBBCCCAAABBBCCC"; varres = Str.match (/aaa/);//No G option is usedConsole.log (RES);//output [' AAA ', index:0, input: ' AAABBBCCCAAABBBCCC '] first represents a matching string, the second represents the position at which the matched string sits at the index, calculated from 0, and the third represents the original string;Example 2:varstr ="AAABBBCCCAAABBBCCC"; varres = Str.match (/aaa/g);//using the G option, the global matchConsole.log (RES);//output [' AAA ', ' AAA '] array of all matching strings2The . Exec function uses the specified regular expression pattern to find matches in the string and returns as an array, and returns a null prototype if not found: Regexp.exec (stringobj) Parameter: RegExp required option, Represents the specified regular expression pattern stringobj, which represents the string return value that needs to be looked up: Returns the first matched string, the location of the string, and an array of the original string, regardless of the G option, but the method can return a sub-match, which is an example 1 where match cannot be used: /c7>varstr ="AAABBBCCCAAABBBCCC"; varREGEXP =/aaa/;//The G option is not used varres =regExp. EXEC (str); Console.log (RES); //output [' AAA ', index:0, input: ' AAABBBCCCAAABBBCCC ']Example 2:varstr ="AAABBBCCCAAABBBCCC"; varREGEXP =/aaa/g;//using the G option varres =regExp. EXEC (str); Console.log (RES); //output [' AAA ', index:0, input: ' AAABBBCCCAAABBBCCC ']Example 3:varstr ="AAABBBCCC1234AAABBBCCC"; varREGEXP =/CCC ([0-9]+) aaa/; varres =regExp. EXEC (str); Console.log (RES); //output [' ccc1234aaa ', ' 1234 ', index:6, input: ' AAABBBCCC1234AAABBBCCC '] the first expression matches the entire regex, and the second represents the content of the () sub-expression that matches the contents ; third and fourth ibid.Example 4:varstr ="AAABBBCCC1234AAABBBCCCAAABBBCCC5678AAABBBCCC"; varreg =/CCC ([0-9]+) aaa/; varres =reg.exec (str); Console.log (RES); //The output is the same as example three, because EXEC returns only the first matched string, and if you want to return it all, you need to use the while loop and the G parameter, as follows varstr ="AAABBBCCC1234AAABBBCCCAAABBBCCC5678AAABBBCCC"; varreg =/CCC ([0-9]+) aaa/G; while(res =reg.exec (str)) {Console.log (res); }//The output is as follows:[' ccc1234aaa ', ' 1234 ', index:6, input: ' AAABBBCCC1234AAABBBCCCAAABBBCCC5678AAABBBCCC '] [' ccc5678aaa ', ' 5678 ', index:28, input: ' AAABBBCCC1234AAABBBCCCAAABBBCCC5678AAABBBCCC ' ] //when using this method, remember to add the G option, otherwise the while loop will not jump out, each time loop the first match, resulting in a dead loop, may be stuck! 3. Test function Prototype: regexp.test (str) parameter: REGEXP indicates that the regular expression pattern stringobj represents a string return value that needs to be matched: Boolean type, Match returns True, otherwise false example 1:varstr ="AAABBBCCCAAABBBCCC"; varReg =/[a-z]+/; varres =reg.test (str); Console.log (RES); //Output TrueExample 2:varstr ="AAABBBCCCAAABBBCCC"; varReg =/[0-9]+/; varres =reg.test (str); Console.log (RES); //Output False //This function is typically used to detect whether a string conforms to a specified rule4. Search function Prototype: Stringobj.search (REGEXP) parameter: REGEXP indicates that the regular expression pattern stringobj represents a string return value that needs to be matched: Returns the position (offset) of the first matched string, starting with 0 Example 1:varstr ="AAABBBCCCAAABBBCCC"; varReg =/ccc/; varres =Str.search (REG); Console.log (RES); //Output 65. Replace function prototype: Stringobj.replace (REGEXP, replacetext) parameter: RegExp represents a regular expression pattern, stringobj represents a string that needs to be matched, ReplaceText represents a replacement for the text content, and ReplaceText can also be a function return value that returns a string: Returns the replacement string Example 1:varstr ="AAABBBCCCAAABBBCCC"; varReg =/aaa/; varres = str.replace (Reg, ' 111); Console.log (res);//return 111BBBCCCAAABBBCCC, replace only the first match, if you need to replace all you need to use the G option5. Split function Prototype: Stringobj.split ([separator[, limit]]) Parameter: separator represents a delimiter (this can also be a regular expression), and stringobj represents a string that needs to be matched. Limit is used to restrict the return value of the number of elements returned from an array: Returns the segmented array example 1:varstr ="AAA,BBB,CCC"; varReg =/,/; varres =Str.split (REG); Console.log (RES); //output [' AAA ', ' BBB ', ' CCC '] //In fact, this can be simply written as var res = Str.split (","); Example 2:varstr ="Aaa,bbb:[email protected] #eee"; varReg =/[,:@#]/; varres =Str.split (REG); Console.log (RES); //output [' AAA ', ' BBB ', ' CCC ', ' ddd ', ' eee '] this is where the power of the regular is.Example 3. varstr ="Aaa,bbb:[email protected] #eee"; varReg =/[,:@#]/; varres = Str.split (Reg, 3); Console.log (RES); //output [' AAA ', ' BBB ', ' CCC ']
Original address: http://www.vxueke.com/2013/04/05/307.htm
node. js "Regular expression function match, test, exec, search, split, replace use detailed"