源代码 :
点击运行
<!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="runoobgoogle\ntaobao\nrunoobweibo"; var n1=str.match(/^runoob/g); // 匹配一个 var n2=str.match(/^runoob/gm); // 多行匹配 document.getElementById("demo1").innerHTML=n1; document.getElementById("demo2").innerHTML=n2; } </script> </body> </html>
运行结果