JavaScript uses RegExp for regular expression matching, and javascriptregexp
This example describes how to use RegExp for regular expression matching in JavaScript. Share it with you for your reference. The specific implementation method is as follows:
<Script type = "text/javascript"> var matchedTimes = 0; // Match one d followed by one or more B's followed by one d // Remember matched B's and the following d // Ignore case myRe = new RegExp ("d (B +) (d) "," ig "); // equivalent to myReg =/d (B +) (d)/ig; myArray = myRe.exe c (" ecDBDsdbbdz "); // ecdbBdbsdbbdz console. log ("Regular Expression String:" + myRe. source); console. log ("Is global? "+ MyRe. global); console. log (" Ignore case? "+ MyRe. ignoreCase); console. log (" Is mulitiline? "+ MyRe. multiline); console. log ("updated"); logInfo (myArray, myRe); myArray = myRe.exe c ("ecDBDsdbbdz"); logInfo (myArray, myRe); function logInfo (myArray, myRe) {matchedTimes ++; console. log ("This is" + matchedTimes + "times match"); console. log ("Original String:" + myArray. input); console. log ("Match Result Array: [" + myArray + "]"); console. log ("The 0- Based index of the match in the string: "+ myArray. index); console. log ("The last matched characters:" + myArray [0]); console. log ("The parenthesized substring matches [1]:" + myArray [1]); console. log ("The parenthesized substring matches [2]:" + myArray [2]); console. log ("The index at which to start the next match:" + myRe. lastIndex); console. log ("-----------------------------------------------" );} MyRe2 =/^ \ w + (\ d *) $/ig console. log ("myRe2:" + myRe2.source); // console. log ("myRe2 matches abc1? "+ MyRe2.test (" abc1 "); // Add this line to view the result. Because it is a global match, lastIndex will change. // The myRe2.test (" abc ") of course, it is false console. log ("myRe2 matches abc? "+ MyRe2.test (" abc "); </script>
I hope this article will help you design javascript programs.