<html>
<body>

<script type="text/javascript">
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));

patt1.compile("d");

document.write(patt1.test("The best things in life are free"));
</script>

</body>
</html>

由于字符串中存在 "e",而没有 "d",以上代码的输出是:

 truefalse

 

compile() 方法用于改变 RegExp。

compile() 既可以改变检索模式,也可以添加或删除第二个参数。