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