JavaScript uses RegExp for regular expression matching, and javascriptregexp

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.