Anonymously Replace string with Regex with simple example

单元源 shanyy163 at 163.com
Fri Sep 7 16:52:30 UTC 2018


No no no, the regex provided here may be included from some where out of your control, all you need to do is replacing the matched part in input string. This is may be useful when you need to do something like this:

const template1 = {
  data: 'USA#1',
  regex: /(\w+)\/(\d+)/
}
const template2 = {
  data: 'Mongo$6',
  regex: /(\w+)\/(\d+)/
}
const template3 = {
  data: 'I am a Javascript Coder age 28',
  regex: /I am a (\w+) Coder age (\d+)/
}

const handler = (template, family, version) => {
  // the replacers here may be more complex, it is just a simple example, nothing more.
  return template.data.replace(template.regex, x => family, x => version);
}

// INPUT: template1, CHINA 99 OUTPUT: CHINA#99
// INPUT: template3, Golang 20 OUTPUT: I am a Golang Coder age 20

Here I use template 1/2/3, it may be some alias instead of real template. For example we may make a map of template, then I just need hander(alias, family, version) to generate some meaningful string. Of cause you could write it in some other way, I just want to show what can this feature do.



> 在 2018年9月7日,下午11:19,Peter Jaszkowiak <p.jaszkow at gmail.com> 写道:
> 
> All you have to do is write the regex differently:
> 
> ```
> input.replace(
>   /(your )(\w+)( from )(\w+)/, 
>   (w, a, b, c, d) => `${a}${b.toUpperCase()}${c}${d.toLowerCase()}`
> );
> ```
> 
> On Fri, Sep 7, 2018, 09:08 sion <shanyy163 at 163.com <mailto:shanyy163 at 163.com>> wrote:
> Emmmmm, pretty sorry for misleading you…
> 
> Your solution will not work if I just provide you a regex without telling you detail information, all I want is replace first and second word matched by the regex, nothing more.
> 
> 
>> 在 2018年9月7日,下午10:57,Peter Jaszkowiak <p.jaszkow at gmail.com <mailto:p.jaszkow at gmail.com>> 写道:
>> 
>> ```
>> input.replace(
>>   /your (\w+) from (\w+)/, 
>>   (w, a, b) => `your ${a.toUpperCase()} from ${b.toLowerCase()}`
>> );
>> ```
>> 
>> On Fri, Sep 7, 2018, 08:49 sion <shanyy163 at 163.com <mailto:shanyy163 at 163.com>> wrote:
>> 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 <p.jaszkow at gmail.com <mailto:p.jaszkow at gmail.com>> 写道:
>>> 
>>> 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 <shanyy163 at 163.com <mailto: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 <mailto:es-discuss at mozilla.org>
>>> https://mail.mozilla.org/listinfo/es-discuss <https://mail.mozilla.org/listinfo/es-discuss>
>> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180908/6e089610/attachment-0001.html>


More information about the es-discuss mailing list