JavaScript 정규식 수정자는 정규식의 선택적 부분이며 대소문자를 구분하지 않고 전역 검색을 수행할 수 있습니다. 수식어를 함께 결합할 수도 있습니다.
다음은 수정자입니다 -
| 수정자 | 설명 |
|---|---|
| g | 전역 일치를 활성화하고 첫 번째 일치에서 중지하는 대신 일치하는 모든 결과를 반환합니다. |
| i | 대소문자를 구분하지 않는 일치를 활성화합니다. |
| m | 여러 줄 일치를 활성화합니다. |
예시
다음은 JavaScript switch 문에서 엄격한 비교를 위한 코드입니다 -
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.result {
font-size: 20px;
font-weight: 500;
}
</style>
</head>
<body>
<h1>JavaScript Regular expression modifiers</h1>
<div class="sample"></div>
<div style="color: green;" class="result"></div>
<button class="Btn">CLICK HERE</button>
<h3>
Click on the above button to see the modifiers operate on the above string
</h3>
<script>
let sampleEle=document.querySelector('.sample');
let resEle = document.querySelector('.result');
let str = '\nHello world. This is a beautiful world';
sampleEle.innerHTML =str;
document.querySelector(".Btn").addEventListener("click", () => {
resEle.innerHTML += '/^Hello/m = ' + str.match(/^Hello/m) + '<br>';
resEle.innerHTML += '/world/g = ' + str.match(/world/g) + '<br>';
resEle.innerHTML += '/WORLD/i = ' + str.match(/WORLD/i) + '<br>';
});
</script>
</body>
</html> 출력
위의 코드는 다음과 같은 출력을 생성합니다 -

'여기를 클릭' 버튼을 클릭하면 -
