代码示例:(标识:eg_js_string_match)
<!DOCTYPE html>
<html>
<body>

<p>单击按钮,在字符串化中执行对字母“ain”(/ain)的全局(/g)搜索,并显示匹配项。</p>

<button onclick="myFunction()">试一试</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>
运行结果: