You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
976 B
44 lines
976 B
/**
|
|
* Obliterator Match Function
|
|
* ===========================
|
|
*
|
|
* Function returning an iterator over the matches of the given regex on the
|
|
* target string.
|
|
*/
|
|
var Iterator = require('./iterator.js');
|
|
|
|
/**
|
|
* Match.
|
|
*
|
|
* @param {RegExp} pattern - Regular expression to use.
|
|
* @param {string} string - Target string.
|
|
* @return {Iterator}
|
|
*/
|
|
module.exports = function match(pattern, string) {
|
|
var executed = false;
|
|
|
|
if (!(pattern instanceof RegExp))
|
|
throw new Error(
|
|
'obliterator/match: invalid pattern. Expecting a regular expression.'
|
|
);
|
|
|
|
if (typeof string !== 'string')
|
|
throw new Error('obliterator/match: invalid target. Expecting a string.');
|
|
|
|
return new Iterator(function () {
|
|
if (executed && !pattern.global) {
|
|
pattern.lastIndex = 0;
|
|
return {done: true};
|
|
}
|
|
|
|
executed = true;
|
|
|
|
var m = pattern.exec(string);
|
|
|
|
if (m) return {value: m};
|
|
|
|
pattern.lastIndex = 0;
|
|
return {done: true};
|
|
});
|
|
};
|