VIK*_*HLI 0 html javascript regex pattern-matching
从https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_replace2获取
现在我想更改字符串中的+91。来自https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_replace2
<!DOCTYPE html>
<html>
<body>
<p>Click the button to replace "blue" with "red" in the paragraph below:</p>
<p id="demo">Mr Blue has a blue house and a blue car.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace(/blue/g, "red");
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
让我们考虑以下字符串。
字符串:- 嗨,我的号码是 +919090909090 和 +9190820209282 等等。
我想要的结果如下:嗨,我的号码是 +91 - 9090909090 和 +91 - 90820209282 等等。
但是当我使用正则表达式模式时,似乎在使用时出现错误
str.replace(/blue/g, "red");
无效的正则表达式:/+91/:没有可重复的内容”
该+符号是正则表达式中的特殊字符
\n\n\n量词 \xe2\x80\x94 匹配一次和无限次,尽可能多的次数,根据需要返回(贪婪)(来自regex101.com)
\n
如果您想与字符串文字匹配,则需要对其进行转义+:
/\\+91/\nRun Code Online (Sandbox Code Playgroud)\n\n会匹配。
\n\n像您希望的那样的示例替换是(再次来自regex101.com)
\n\n/\\+91/\nRun Code Online (Sandbox Code Playgroud)\r\n| 归档时间: |
|
| 查看次数: |
918 次 |
| 最近记录: |