js正则常用方法

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>W3Cschool教程(w3cschool.cn)</title>
</head>
<body>
<h1>我的第一个 Web 页面</h1>
<button id = "demo">clock me</button>
<script>
document.getElementById("demo").addEventListener("click",myfun);
function myfun(){
//JavaScript 正则表达式
//定义:正则表达式是由一个字符序列形成的搜索模式
//语法:pattern/modifiers

//test方法
var a = new RegExp("mike");
document.write(a.test("mike is the best"));

//exec方法
var b = new RegExp("love");
document.write(b.exec("mike have love a girl some years ago"));

//compile方法
//replace方法
var c="her name is lina";
patt=/(li)?na/g;
patt.compile(patt);
document.write(c.replace(patt,"nana"));

//search方法
var d = "i wish lina has a happy life everyday";
document.write(d.search("lina"));

}

</script>

</body>
</html>

原文地址:https://www.cnblogs.com/mike1314/p/8012753.html