This is just a very simple example, of cause you could write it like that.

Maybe my second example is unsuitable, how about this one:

input.replace(/your \w+ from \w+/, a => a.toUpperCase(), b => b.toLowerCase());

INPUT: your friend from USA  OUTPUT: your FRIEND from usa

Actually, I am not very familiar with regex, I wonder if there is any 
performance issue if we support this kind of feature.



> 在 2018年9月7日,下午10:13,Peter Jaszkowiak <[email protected]> 写道:
> 
> 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 
> totally useless:
> 
> ```
> input.replace(
>   /your (\w+) from (\w+)/g, 
>   (whole, a, b) => 'your book from amazon'
> );
> ```
> 
> On Fri, Sep 7, 2018, 07:23 sion <[email protected] 
> <mailto:[email protected]>> 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
> [email protected] <mailto:[email protected]>
> https://mail.mozilla.org/listinfo/es-discuss 
> <https://mail.mozilla.org/listinfo/es-discuss>

_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to