Skip to content

Instantly share code, notes, and snippets.

@pentaphobe
Created August 1, 2017 10:27
Show Gist options
  • Save pentaphobe/90fb32d2818f3e6c23d9af8f6a8c021b to your computer and use it in GitHub Desktop.
Save pentaphobe/90fb32d2818f3e6c23d9af8f6a8c021b to your computer and use it in GitHub Desktop.
JS regex matchAll
'use strict';
function matchAll(re, str) {
let result = [], tmp;
while ( (tmp = re.exec(str)) !== null ) {
tmp.shift();
result = result.concat(tmp);
}
return result;
}
module.exports = {
matchAll
};
const { matchAll } = require('./matchAll');
let reKeyValuePair = /\s*#(\w+)\s*:\s*([^#]+)\s*/g;
let testString = '#hoo: foo #howdy: noodle face things #stuff: otherstuff!';
matchAll(reKeyValuePair, testString);
// output: ["hoo", "foo ", "howdy", "noodle face things ", "stuff", "otherstuff!"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment