Regex on substrings

Mike Samuel mikesamuel at
Thu Jun 2 10:17:26 PDT 2011

2011/6/2 Peter van der Zee <ecma at>:
> A problem I faced recently is the inability to apply regular
> expressions to a substring of a string without explicitly taking the
> substring first. So I'm wondering how much trouble it would be to
> extend the RegExp api to this...
> RegExp.prototype.test = function(string[, start=0[, stop=string.length]]){ };
> RegExp.prototype.exec = function(string[, start=0[, stop=string.length]]){ };

What do the ^ and $ assertions in non-multiline mode mean when start
and stop are not [0, string.length)?
Do they match at the beginning and end of the substring?

> Optionally, it might be handy to have .test return a number,
> indicating the length of the (first) match. If zero, there was no
> match. This would however break with scripts that explicitly check for
> === false.

Using zero to indicate that there is no match will conflate zero
length matches with no match.

    /\b/.test("a-b") === true

More information about the es-discuss mailing list