JavaScript Regular expression pattern matching (3)-greedy mode and lazy mode

Source: Internet
Author: User

1 varpattern=/[a-z]+/;//greedy mode is used here.2 varStr= ' ABCDEFG '; 3Alert (str.replace (Pattern, ' 1 '));//all the strings become 1.4 5 varpattern=/[a-z]+?/;//The lazy mode is used here,6 varStr= ' ABCDEFG '; 7Alert (str.replace (Pattern, ' 1 '));//only the first character becomes 1, and there is no match behind it.8     9 varpattern=/[a-z]+?/;//turn on global, and use lazy mode,Ten varStr= ' ABCDEFG ';  OneAlert (str.replace (Pattern, ' 1 '));//each letter was replaced by a 1 A  - varPATTERN=/6 (. *) 6/;//using the greedy mode, - varStr= ' 6google6 6google6 6google6 ';//matched to the Google6 6google6 6google thedocument.write (str.replace (Pattern, ' <strong>$1<strong> '));//results: <strong>google6 6google6 6google<strong> -  - varPATTERN=/6 (. *?) 6/;//using the lazy mode, - varStr= ' 6google6 6google6 6google6 ';  +document.write (str.replace (Pattern, ' <strong>$1<strong> '));//results:<strong>google<strong> 6google6 6google6 -  + varPATTERN=/6 (. *?) 6/g;//use lazy mode to turn on the global A varStr= ' 6google6 6google6 6google6 ';  atdocument.write (str.replace (Pattern, ' <strong>$1<strong> ')));  - //Results:<strong>google<strong> <strong>google<strong> <strong>google<strong>  - //The results are correct -  - varPATTERN=/6 ([^6]*) 6/g;//Another kind of inertia, masking 6 of the match, that is, the containing characters on both sides - varStr= ' 6google6 6google6 6google6 ';  indocument.write (str.replace (Pattern, ' <strong>$1<strong> '));

JavaScript Regular expression pattern matching (3)-greedy mode and lazy mode

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.