使用javascript将字符串中的多个加法符号(+)替换为单个加法符号(+)

2w3rbyxf  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(340)

在下面的代码中,我尝试使用javascript将字符串中的多个加法符号替换为单个加法符号。我没有得到正确的正则表达式,请帮助!
示例:此++是+我的+文本-->此+是+我的+文本
代码:

function doThis(){
document.getElementById("result").value = document.getElementById("TextInput").value.replace(/++/g,'+');
}
<html>
<head>
</head>
<body>

<textarea type="number" autocomplete="off" cols="30" id="TextInput" name="message" rows="10" style="width: 100%;">This++is+my+++++text</textarea><br><br>

<input class="button" type="button" value="Press" onclick="doThis()"><br/><br/>

Result: <input type="text" id="result">

</body>
</html>
t3psigkw

t3psigkw1#

你需要逃离这个世界 + .

function doThis() {
  document.getElementById("result").value =
    document.getElementById("TextInput").value.replace(/\++/g, '+');
}
<textarea type="number" autocomplete="off" cols="30" id="TextInput" name="message" rows="10" style="width: 100%;">This++is+my+++++text</textarea><br><br>
<input class="button" type="button" value="Press" onclick="doThis()"><br/><br/> Result: <input type="text" id="result">

相关问题