源代码 :
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>开源教程(ossoft.cn)</title> </head> <body> <p>单击按钮显示匹配项:</p> <p id="demo1"></p> <p id="demo2"></p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var str="Google runoob taobao runoob"; var n1=str.match(/runoob/); // 查找第一次匹配项 var n2=str.match(/runoob/g); // 查找所有匹配项 document.getElementById("demo1").innerHTML=n1; document.getElementById("demo2").innerHTML=n2; } </script> </body> </html>
运行结果