Created
June 17, 2014 05:33
-
-
Save Williammer/d89d90a85fa5f618bd80 to your computer and use it in GitHub Desktop.
jsRegExp.match.js - pattern matches for string and tag, with local capturing feature with parentheses and global/local group match with exec in loop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //// warm-ups | |
| //var str = 'opacity=300'; | |
| //var reg = /opacity=([\d]{1,2})/; | |
| //var res = (parseFloat(str.match(reg)[1]) / 100) + ""; | |
| //html tag match | |
| var res = []; | |
| var html = "<div class='test'><b>Hello</b> <i>world!</i></div>"; | |
| var tag = /<(\/?)(\w+)([^>]*?)>/g, | |
| match; | |
| //var num = 0; | |
| while ((match = tag.exec(html)) !== null) { | |
| res.push(match); //exec() will perform local match for each global matched result; while match() perform global match and return the list of matching results instead of capture. | |
| } | |
| //match = html.match(tag); | |
| console.dir(res); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment