Computer >> Máy Tính >  >> Lập trình >> Javascript

Giải thích cờ 'dotAll' cho các biểu thức chính quy trong JavaScript


Cờ dotAll trả về true hoặc false tùy thuộc vào việc cờ s đã được đặt trong biểu thức chính quy hay chưa.

Sau đây là mã để triển khai cờ dotAll cho các biểu thức chính quy trong JavaScript -

Ví dụ

<!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>dotAll flag for regular expressions</h1>
<div style="color: green;" class="result"></div>
<button class="btn">CLICK HERE</button>
<h3>
Click on the above button to know if s flag has been set in regex or not
</h3>
<script>
   let btnEle = document.querySelector(".btn");
   let resEle = document.querySelector(".result");
   const regex = new RegExp("Hello", "s");
   btnEle.addEventListener("click", () => {
      resEle.innerHTML = "The s flag has been set : " + regex.dotAll;
   });
</script>
</body>
</html>

Đầu ra

Đoạn mã trên sẽ tạo ra kết quả sau -

Giải thích cờ  dotAll  cho các biểu thức chính quy trong JavaScript

Khi nhấp vào nút 'BẤM VÀO ĐÂY' -

Giải thích cờ  dotAll  cho các biểu thức chính quy trong JavaScript