Created
August 1, 2017 10:27
-
-
Save pentaphobe/90fb32d2818f3e6c23d9af8f6a8c021b to your computer and use it in GitHub Desktop.
JS regex matchAll
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
'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 | |
}; |
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
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