在str.match javascript的字符串中匹配'+'

时间:2017-05-12 05:18:46

标签: javascript regex string

我在看这个例子:

https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_match_regexp

<!DOCTYPE html>
<html>
<body>

<p>Click the button to perform a global search for the letters "ain+" in a string, and display the matches.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var str = "The rain in SPAIN stays mainly in the plain"; 
    var res = str.match(/ain+/g);
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

这就是我需要的,但是如果我在'ain'的末尾添加一个'+'它不起作用,我应该怎么做?

2 个答案:

答案 0 :(得分:-1)

/ ain + /匹配ain,ainn,ainnn,ainnnn ......&#34; ai&#34;和一个或多个&#34; n&#34;。

答案 1 :(得分:-2)

function myFunction() {
  var str = "The rain in SPAIN stays mainly in the plain ain+";
  var res = str.match(/ain\+/g);
  document.getElementById("demo").innerHTML = "In the sentence: [" + str + " ] " + res + " was found.";
}
<p>Click the button to perfom a global search for the letters "ain+" in a string, and display the matches.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>