String.prototype.until

Axel Rauschmayer axel at rauschma.de
Mon Jan 2 09:55:14 PST 2012


Isn’t that usually better handled via a regular expression?

One of the use cases for quasis [1][2] is to make it easy to insert literal text into a regular expression. That seems pertinent here. Example:

    re`\d+(${localeSpecificDecimalPoint}\d+)?`

The text in the variable localeSpecificDecimalPoint is matched literally by the regular expression produced by re``.

[1] http://wiki.ecmascript.org/doku.php?id=harmony:quasis
[2] http://www.2ality.com/2011/09/quasi-literals.html


On Jan 2, 2012, at 18:03 , Adam Shannon wrote:

> Hello all,
> 
> I recently ran into a situation where I would like to obtain a
> substring from the beginning until the first encounter with another
> substring. This promoted me to write a simple function, called until
> and I wondered if it would be something to add with the other string
> extras for ES.next.
> 
> It could be defined as acting the same way as the following code:
> 
> String.prototype.until = function (needle) {
>  return this.substr(0, this.indexOf(needle));
> }

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com





More information about the es-discuss mailing list