<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">There is some errors by the way, missing # in template1.regex and $ in template2.regex, sorry for that.<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">在 2018年9月8日,上午12:52,单元源 <<a href="mailto:shanyy163@163.com" class="">shanyy163@163.com</a>> 写道:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=gb2312" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><span style="font-size: 14px;" class="">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:</span><div class=""><br class=""></div><div class=""><div style="color: rgb(248, 248, 242); background-color: rgb(0, 0, 0); font-family: Menlo, Monaco, "Courier New", monospace; font-size: 14px; line-height: 21px; white-space: pre;" class=""><div class=""><span style="color: #66d9ef;font-style: italic;" class="">const</span> <span style="color: #678cb1;" class="">template1</span> <span style="color: #f92672;" class="">=</span> {</div><div class="">  data: <span style="color: #e6db74;" class="">'USA#1'</span>,</div><div class="">  regex:<span style="color: #f6aa11;" class=""> /(</span><span style="color: #be84ff;" class="">\w</span><span style="color: #f92672;" class="">+</span><span style="color: #f6aa11;" class="">)</span><span style="color: #be84ff;" class="">\/</span><span style="color: #f6aa11;" class="">(</span><span style="color: #be84ff;" class="">\d</span><span style="color: #f92672;" class="">+</span><span style="color: #f6aa11;" class="">)/</span></div><div class="">}</div><div class=""><span style="color: #66d9ef;font-style: italic;" class="">const</span> <span style="color: #678cb1;" class="">template2</span> <span style="color: #f92672;" class="">=</span> {</div><div class="">  data: <span style="color: #e6db74;" class="">'Mongo$6'</span>,</div><div class="">  regex:<span style="color: #f6aa11;" class=""> /(</span><span style="color: #be84ff;" class="">\w</span><span style="color: #f92672;" class="">+</span><span style="color: #f6aa11;" class="">)</span><span style="color: #be84ff;" class="">\/</span><span style="color: #f6aa11;" class="">(</span><span style="color: #be84ff;" class="">\d</span><span style="color: #f92672;" class="">+</span><span style="color: #f6aa11;" class="">)/</span></div><div class="">}</div><div class=""><span style="color: #66d9ef;font-style: italic;" class="">const</span> <span style="color: #678cb1;" class="">template3</span> <span style="color: #f92672;" class="">=</span> {</div><div class="">  data: <span style="color: #e6db74;" class="">'I am a Javascript Coder age 28'</span>,</div><div class="">  regex:<span style="color: #f6aa11;" class=""> /I am a (</span><span style="color: #be84ff;" class="">\w</span><span style="color: #f92672;" class="">+</span><span style="color: #f6aa11;" class="">) Coder age (</span><span style="color: #be84ff;" class="">\d</span><span style="color: #f92672;" class="">+</span><span style="color: #f6aa11;" class="">)/</span></div><div class="">}</div><br class=""><div class=""><span style="color: #66d9ef;font-style: italic;" class="">const</span> <span style="color: #a6e22e;" class="">handler</span> <span style="color: #f92672;" class="">=</span> <span style="color: #ffffff;" class="">(</span><span style="color: #fd971f;font-style: italic;" class="">template</span>, <span style="color: #fd971f;font-style: italic;" class="">family</span>, <span style="color: #fd971f;font-style: italic;" class="">version</span><span style="color: #ffffff;" class="">)</span> <span style="color: #66d9ef;font-style: italic;" class="">=></span> {</div><div class="">  <span style="color: #75715e;" class="">// the replacers here may be more complex, it is just a simple example, nothing more.</span></div><div class="">  <span style="color: #f92672;" class="">return</span> <span style="color: #678cb1;" class="">template</span>.data.<span style="color: #66d9ef;" class="">replace</span><span style="color: #ffffff;" class="">(</span><span style="color: #678cb1;" class="">template</span>.<span style="color: #678cb1;" class="">regex</span>, <span style="color: #fd971f;font-style: italic;" class="">x</span> <span style="color: #66d9ef;font-style: italic;" class="">=></span> <span style="color: #678cb1;" class="">family</span>, <span style="color: #fd971f;font-style: italic;" class="">x</span> <span style="color: #66d9ef;font-style: italic;" class="">=></span> <span style="color: #678cb1;" class="">version</span><span style="color: #ffffff;" class="">)</span>;</div><div class="">}</div><br class=""><div class=""><span style="color: #75715e;" class="">// INPUT: template1, CHINA 99 OUTPUT: CHINA#99</span></div><div class=""><span style="color: #75715e;" class="">// INPUT: template3, Golang 20 OUTPUT: I am a Golang Coder age 20</span></div></div></div><div class=""><div class=""><br class=""></div><div style="font-size: 14px;" class="">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 <b class="">hander(alias, family, version)</b> 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.</div><div style="font-size: 14px;" class=""><br class=""></div><div style="font-size: 14px;" class=""><br class=""></div><div class=""><br class=""><div class=""><div class=""><blockquote type="cite" class=""><div class="">在 2018年9月7日,下午11:19,Peter Jaszkowiak <<a href="mailto:p.jaszkow@gmail.com" class="">p.jaszkow@gmail.com</a>> 写道:</div><br class="Apple-interchange-newline"><div class=""><div dir="auto" class=""><div class="">All you have to do is write the regex differently:<div dir="auto" class=""><br class=""></div><div dir="auto" class=""><div dir="auto" class="">```</div><div dir="auto" class="">input.replace(</div><div dir="auto" class="">  /(your )(\w+)( from )(\w+)/, </div><div dir="auto" class="">  (w, a, b, c, d) => `${a}${b.toUpperCase()}${c}${d.toLowerCase()}`</div><div dir="auto" class="">);</div><div dir="auto" class="">```</div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Fri, Sep 7, 2018, 09:08 sion <<a href="mailto:shanyy163@163.com" class="">shanyy163@163.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">Emmmmm, pretty sorry for misleading you…</div><div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">在 2018年9月7日,下午10:57,Peter Jaszkowiak <<a href="mailto:p.jaszkow@gmail.com" target="_blank" rel="noreferrer" class="">p.jaszkow@gmail.com</a>> 写道:</div><br class="m_-4012219193582376781Apple-interchange-newline"><div class=""><div dir="auto" class=""><div class="">```</div><div dir="auto" class="">input.replace(</div><div dir="auto" class="">  /your (\w+) from (\w+)/, </div><div dir="auto" class="">  (w, a, b) => `your ${a.toUpperCase()} from ${b.toLowerCase()}`</div><div dir="auto" class="">);<br class=""></div><div dir="auto" class="">```<br class=""><br class=""><div class="gmail_quote" dir="auto"><div dir="ltr" class="">On Fri, Sep 7, 2018, 08:49 sion <<a href="mailto:shanyy163@163.com" target="_blank" rel="noreferrer" class="">shanyy163@163.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">This is just a very simple example, of cause you could write it like that.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div class="">Maybe my second example is unsuitable, how about this one:</div></div><div class=""><br class=""></div><div class=""><pre style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:16px;white-space:pre-wrap;break-inside:avoid;direction:ltr;margin-top:0px;margin-bottom:1.275em;padding:0.85em 1em;border:none;color:rgb(51,51,51);overflow:auto;background-color:rgb(247,247,247);font-variant-ligatures:normal;letter-spacing:0.2px;background-position:initial initial;background-repeat:initial initial" class=""><code class="m_-4012219193582376781m_850565665732755845lang-javascript" style="box-sizing:border-box;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:0.85em;break-inside:avoid;direction:ltr;margin:0px;padding:0px;border:none;color:inherit;background:0px 0px;display:inline;max-width:initial;overflow:initial;line-height:inherit;white-space:pre-wrap">input.replace(<span class="m_-4012219193582376781m_850565665732755845hljs-regexp" style="box-sizing:border-box;font-size:inherit;color:rgb(200,40,41)">/your \w+ from \w+/</span>, a => a.toUpperCase(), b => b.toLowerCase());</code></pre><div class=""><br class=""></div></div><div class=""><pre style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;break-inside:avoid;direction:ltr;margin-top:0px;margin-bottom:1.275em;padding:0.85em 1em;border:none;color:rgb(51,51,51);overflow:auto;background-color:rgb(247,247,247);font-variant-ligatures:normal;letter-spacing:0.2px" class=""><font size="2" class="">INPUT: your friend from USA  OUTPUT: your FRIEND from usa</font></pre></div><div class=""><br class=""></div><div class="">Actually, I am not very familiar with regex, I wonder if there is any performance issue if we support this kind of feature.</div><div class=""><br class=""></div><div class=""><br class=""></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">在 2018年9月7日,下午10:13,Peter Jaszkowiak <<a href="mailto:p.jaszkow@gmail.com" rel="noreferrer noreferrer" target="_blank" class="">p.jaszkow@gmail.com</a>> 写道:</div><br class="m_-4012219193582376781m_850565665732755845Apple-interchange-newline"><div class=""><div dir="auto" class="">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:<div dir="auto" class=""><br class=""></div><div dir="auto" class="">```</div><div dir="auto" class="">input.replace(</div><div dir="auto" class="">  /your (\w+) from (\w+)/g, </div><div dir="auto" class="">  (whole, a, b) => 'your book from amazon'</div><div dir="auto" class="">);<br class=""></div><div dir="auto" class="">```<br class=""><br class=""><div class="gmail_quote" dir="auto"><div dir="ltr" class="">On Fri, Sep 7, 2018, 07:23 sion <<a href="mailto:shanyy163@163.com" rel="noreferrer noreferrer" target="_blank" class="">shanyy163@163.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class=""><br class=""></div><div class="">Is it possible to enhance the string.prototype.replace(regex, func) ?</div><div class=""><br class=""></div><div class="">By now, we can do something like this:</div><div class=""><br class=""></div><div class=""><pre style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:16px;white-space:pre-wrap;break-inside:avoid;direction:ltr;margin-top:0px;margin-bottom:1.275em;padding:0.85em 1em;border:none;color:rgb(51,51,51);overflow:auto;background-color:rgb(247,247,247);font-variant-ligatures:normal;letter-spacing:0.2px;background-position:initial initial;background-repeat:initial initial" class=""><code class="m_-4012219193582376781m_850565665732755845m_-862626295884998020lang-javascript" style="box-sizing:border-box;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:0.85em;break-inside:avoid;direction:ltr;margin:0px;padding:0px;border:none;color:inherit;background:0px 0px;display:inline;max-width:initial;overflow:initial;line-height:inherit;white-space:pre-wrap">input.replace(<span class="m_-4012219193582376781m_850565665732755845m_-862626295884998020hljs-regexp" style="box-sizing:border-box;font-size:inherit;color:rgb(200,40,41)">/(^|_)[a-z]/g</span>, a => a[a.length - <span class="m_-4012219193582376781m_850565665732755845m_-862626295884998020hljs-number" style="box-sizing:border-box;font-size:inherit;color:rgb(245,135,31)">1</span>].toUpperCase());</code></pre><div class=""><pre style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;break-inside:avoid;direction:ltr;margin-top:0px;margin-bottom:1.275em;padding:0.85em 1em;border:none;color:rgb(51,51,51);overflow:auto;background-color:rgb(247,247,247);font-variant-ligatures:normal;letter-spacing:0.2px" class=""><font size="2" class="">INPUT: ab_cd_ef     OUTPUT: abCdEf</font></pre><div class=""><br class=""></div></div><div class=""><br class=""></div><div class="">However, i want something more powerfull, like this:</div></div><div class=""><br class=""></div><div class=""><pre style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:16px;white-space:pre-wrap;break-inside:avoid;direction:ltr;margin-top:0px;margin-bottom:1.275em;padding:0.85em 1em;border:none;color:rgb(51,51,51);overflow:auto;background-color:rgb(247,247,247);font-variant-ligatures:normal;letter-spacing:0.2px;background-position:initial initial;background-repeat:initial initial" class=""><code class="m_-4012219193582376781m_850565665732755845m_-862626295884998020lang-javascript" style="box-sizing:border-box;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:0.85em;break-inside:avoid;direction:ltr;margin:0px;padding:0px;border:none;color:inherit;background:0px 0px;display:inline;max-width:initial;overflow:initial;line-height:inherit;white-space:pre-wrap">input.replace(<span class="m_-4012219193582376781m_850565665732755845m_-862626295884998020hljs-regexp" style="box-sizing:border-box;font-size:inherit;color:rgb(200,40,41)">/your (\w+) from (\w+)/g</span>, a => 'book', b => 'amazon’);</code></pre><pre style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:16px;white-space:pre-wrap;break-inside:avoid;direction:ltr;margin-top:0px;margin-bottom:1.275em;padding:0.85em 1em;border:none;color:rgb(51,51,51);overflow:auto;background-color:rgb(247,247,247);font-variant-ligatures:normal;letter-spacing:0.2px;background-position:initial initial;background-repeat:initial initial" class=""><code class="m_-4012219193582376781m_850565665732755845m_-862626295884998020lang-javascript" style="box-sizing:border-box;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:0.85em;break-inside:avoid;direction:ltr;margin:0px;padding:0px;border:none;color:inherit;background:0px 0px;display:inline;max-width:initial;overflow:initial;line-height:inherit;white-space:pre-wrap">INPUT: your friend from china OUTPUT: your book from amazon</code></pre><div class=""><br class=""></div></div><div class="">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.</div><div class=""><br class=""></div></div>_______________________________________________<br class="">
es-discuss mailing list<br class="">
<a href="mailto:es-discuss@mozilla.org" rel="noreferrer noreferrer noreferrer" target="_blank" class="">es-discuss@mozilla.org</a><br class="">
<a href="https://mail.mozilla.org/listinfo/es-discuss" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank" class="">https://mail.mozilla.org/listinfo/es-discuss</a><br class="">
</blockquote></div></div></div>
</div></blockquote></div><br class=""></div></blockquote></div></div></div>
</div></blockquote></div><br class=""></div></blockquote></div></div></div>
</div></blockquote></div><br class=""></div></div></div></div></div></blockquote></div><br class=""></body></html>