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

时间:2017-02-23 11:03:21

标签: javascript html 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>

让我们考虑以下字符串。

字符串: - 您好我的电话号码是+919090909090和+9190820209282 ......等等。

我想要的结果如下:嗨我的电话号码是+91 - 9090909090和+91 - 90820209282 ......等等。

但是当我使用正则表达式模式时,我使用时似乎通过错误 str.replace(/blue/g, "red");

  

正则表达式无效:/ + 91 /:无需重复“

0 个答案:

没有答案