Enhancement of RegExp replacement
p.jaszkow at gmail.com
Fri Sep 7 14:13:00 UTC 2018
You do know that all capture groups are passed to the function, right? You
can write your second example like this, even though the capture groups are
/your (\w+) from (\w+)/g,
(whole, a, b) => 'your book from amazon'
On Fri, Sep 7, 2018, 07:23 sion <shanyy163 at 163.com> wrote:
> Is it possible to enhance the string.prototype.replace(regex, func) ?
> By now, we can do something like this:
> input.replace(/(^|_)[a-z]/g, a => a[a.length - 1].toUpperCase());
> INPUT: ab_cd_ef OUTPUT: abCdEf
> However, i want something more powerfull, like this:
> input.replace(/your (\w+) from (\w+)/g, a => 'book', b => 'amazon’);
> INPUT: your friend from china OUTPUT: your book from amazon
> As you can see, I just want the replace could replace multi fragments at
> the same time. And I don’t think there is any conflicts with other
> principles of string.prototype.replace.
> es-discuss mailing list
> es-discuss at mozilla.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the es-discuss