JS Regex - 替换字符串中的多个实例(JS Regex - Replace multiple instances in a string)
我有这个字符串:
var str = '"testFN, II testLN, II" < test1@test.com>, "testFN, Cal testLN, Cal" < test2@test.com>';
而我在做
str.replace(/(".*?),(.*?")/g, '$1__comma_$2');
用字符串(__ comma_)替换多个逗号(,),仅在“”内。
事件虽然我给它/ g它没有替换所有实例。 它仅替换每个实例的第一个匹配项。
实际结果:“testFN__comma_ II testLN,II”<test1@test.com>,“testFN__comma_ testLN,Cal”<test2@test.com>预期结果:“testFN__comma_ II testLN__comma_ II”<test1@test.com>,“testFN__comma_ Cal testLN__comma_ Cal“<test2@test.com>
你能否提出任何我错过的建议?
I have this string:
var str = '"testFN, II testLN, II" < test1@test.com>, "testFN, Cal testLN, Cal" < test2@test.com>';
and I am doing
str.replace(/(".*?),(.*?")/g, '$1__comma_$2');
to replace multiple commas(,) with in a string(__comma_), only inside "".
Event though I given /g its not replacing the all instances. It replace only first match of each instance.
Actual Result: "testFN__comma_ II testLN, II" < test1@test.com>, "testFN__comma_ testLN, Cal" < test2@test.com> Expected Result: "testFN__comma_ II testLN__comma_ II" < test1@test.com>, "testFN__comma_ Cal testLN__comma_ Cal" < test2@test.com>
Can you please give any suggestions that I am missing anything ?
原文:https://stackoverflow.com/questions/26307794