Last active
July 17, 2019 11:43
-
-
Save branneman/821b66dfc49f65703b68 to your computer and use it in GitHub Desktop.
JavaScript RegExp: Get all matches and capture groups
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' | |
const { isNull, isStr, isRegExp } = require('./types') | |
module.exports = matchAll | |
/** | |
* RegExp: Get all matches and capturing groups | |
* @param {RegExp} re | |
* @param {String} str | |
* @returns {Array<Array<String>>} | |
*/ | |
function matchAll(re, str) { | |
if (!isRegExp(re) || !isStr(str)) | |
throw new Error('matchAll(RegExp, String): received incorrect types') | |
const matches = [] | |
let groups | |
while (!isNull((groups = re.exec(str)))) { | |
matches.push(Array.from(groups)) | |
} | |
return matches | |
} |
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' | |
const matchAll = require('./matchAll') | |
describe('matchAll()', () => { | |
it('throws on incorrect regexp', () => { | |
const re = {} | |
const str = '123' | |
expect(() => matchAll(re, str)).toThrowError(/incorrect types/) | |
}) | |
it('throws on incorrect string', () => { | |
const re = /[a-z]/ | |
const str = 123 | |
expect(() => matchAll(re, str)).toThrowError(/incorrect types/) | |
}) | |
it('returns multiple matches', () => { | |
const re = /#{1,6} .+/g | |
const str = `asd | |
# one | |
body 1 | |
## two | |
body 2 | |
body 2 more | |
### three | |
body 3` | |
const result = matchAll(re, str) | |
expect(result).toEqual([['# one'], ['## two'], ['### three']]) | |
}) | |
it('returns multiple capuring groups', () => { | |
const re = /(#{1,6}) (.+)/g | |
const str = `asd | |
# one | |
body 1 | |
## two | |
body 2 | |
body 2 more | |
### three | |
body 3` | |
const result = matchAll(re, str) | |
expect(result).toEqual([ | |
['# one', '#', 'one'], | |
['## two', '##', 'two'], | |
['### three', '###', 'three'] | |
]) | |
}) | |
}) |
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' | |
module.exports = { isNull, isStr, isRegExp } | |
function isNull(val) { | |
return val === null | |
} | |
function isStr(val) { | |
return typeof val === 'string' | |
} | |
function isArr(val) { | |
return Array.isArray(val) | |
} | |
function isObj(val) { | |
return val === Object(val) && !isArr(val) && !isFunc(val) && !isSymbol(val) | |
} | |
function isRegExp(val) { | |
return isObj(val) && val instanceof RegExp | |
} | |
function isFunc(val) { | |
return typeof val === 'function' | |
} | |
function isSymbol(val) { | |
return typeof val === 'symbol' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment