如何使用正则表达式模式替换+91?

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/:没有可重复的内容”

chr*_*con 5

+符号是正则表达式中的特殊字符

\n\n
\n

量词 \xe2\x80\x94 匹配一次和无限次,尽可能多的次数,根据需要返回(贪婪)(来自regex101.com

\n
\n\n

如果您想与字符串文字匹配,则需要对其进行转义+

\n\n
/\\+91/\n
Run Code Online (Sandbox Code Playgroud)\n\n

会匹配。

\n\n

像您希望的那样的示例替换是(再次来自regex101.com

\n\n

\r\n

\n