Skip to content

Instantly share code, notes, and snippets.

@devhero
Last active October 2, 2017 10:29
Show Gist options
  • Save devhero/51553d0677592607ffc5967053fd6172 to your computer and use it in GitHub Desktop.
Save devhero/51553d0677592607ffc5967053fd6172 to your computer and use it in GitHub Desktop.
regex tips
// search 2 words multiline
(?=.*\bWORD1\b).*\n*(?=.*\bWORD2\b).*
// exclude word
\b((?!word)\w)+\b
// iterate over matches and do something with group's result
var myString = "something format_abc";
var myRegexp = /(?:^|\s)format_(.*?)(?:\s|$)/g;
match = myRegexp.exec(myString);
while (match != null) {
// matched text: match[0]
// match start: match.index
// capturing group n: match[n]
console.log(match[0])
match = myRegexp.exec(myString);
}
// iterate over matches and collect group's result
var matches = []
while (match = myRegex.exec(myStr)) matches.push(match[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment