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> 출력

"여기를 클릭하십시오" 버튼을 클릭하면 -
