我需要替换_New + Delivery一词;用逗号(',')组成的字符串中
样本输入字符串:XP_New+Delivery;HP_New+Delivery;LA_New;
预期输出:XP,HP,LA_New;
但是它返回的是与输出相同的输入,而不是替换任何主意吗?
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace(new RegExp('_New+Delivery;', 'gi'), ',');
document.getElementById("demo").innerHTML = res;
}
<p id="demo">XP_New+Delivery;HP_New+Delivery;LA_New;</p>
<button onclick="myFunction()">Try it</button>
最佳答案
试试这个:
var str = "XP_New+Delivery;HP_New+Delivery;LA_New;";
var res = str.replace(/(_New\+Delivery;)/g, ",");
console.log(res);