JavaScript compile() 方法
JavaScript compile() 方法用于在脚本执行期间编译正则表达式。它在 JavaScript 1.5 版本中弃用。
以下是 JavaScript compile() 方法的代码 −
示例
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
</style>
</head>
<body>
<h1>compile() Method</h1>
<p class="sample">
That man won the race and the woman over there came second
</p>
<h3>After compile()</h3>
<p class="result"></p>
<button class="Btn">Click Here</button>
<h3>Click the above button to use compile the regex to make changes</h3>
<script>
let sampleEle = document.querySelector(".sample");
let result = document.querySelector(".result");
document.querySelector(".Btn").addEventListener("click", () => {
var str = sampleEle.innerHTML;
var pattern = /man/g;
var str2 = str.replace(pattern, "person");
result.innerHTML = str2 + "<br>";
pattern = /(wo)?man/g;
pattern.compile(pattern);
str2 = str.replace(pattern, "person");
result.innerHTML += str2;
});
</script>
</body>
</html>输出

点击“单击此处”按钮 −

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP