Last active
August 29, 2015 14:17
Regex multiple matching with exec via http://stackoverflow.com/a/2295943/2668831
This file contains 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
myre = /]\(/g | |
mystr = 'hello ]( world ]( i ]( said ](' | |
while ((match = myre.exec(mystr)) != null) { | |
console.log(myre.lastIndex) | |
} | |
/* | |
8 | |
17 | |
22 | |
30 | |
*/ | |
while ((match = myre.exec(mystr)) != null) { | |
console.log(match.index + match[0].length) | |
} | |
/* | |
8 | |
17 | |
22 | |
30 | |
*/ | |
// mystr.length | |
// 30 | |
/* Note that if assigning a value, repeated overwriting lets you use the last output of the loop | |
- can be handy, e.g. find the definitive separator in a markdown link: */ | |
var mylink = "" | |
// strip off ; | |
// "hello ]( world ]( i ]( said " | |
var link_href = link_inner.substring(link_sep); | |
// "https://gist.github.com" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment