leodutra
6/21/2016 - 5:39 AM

Simple multiple Regular Expresion (Regex) matches.

Simple multiple Regular Expresion (Regex) matches.

function regexMultiMatch(str, regex, fn) {
  
  // clone for no regex.lastIndex problems
  var cloneRegExp = new RegExp(regex.source, regex.flags || 
    (regex.global ? 'g' : '') + (regex.ignoreCase ? 'i' : '') + (regex.multiline ? 'm' : ''))
    
  fn.apply(null, regex.exec(str))
  if (regex.global) {
    var match 
    while((match = regex.exec(str))) fn.apply(null, match)
  }
}