Unicode 屬性轉義 JavaScript 正則表示式


Unicode 屬性轉義正則表示式允許我們透過使用 u 標誌基於它們的 Unicode 屬性來匹配字元。

示例

下面是一個示例 −

 線上演示

<!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;
   }
   .sample, .result {
      font-size: 20px;
      font-weight: 500;
}
</style>
</head>
<body>
<h1>Unicode Property Escapes JavaScript Regular Expressions</h1>
<div class="sample">Hello 😆😀 World 🙂😊</div>
<div style="color: green;" class="result"></div>
<button class="btn">CLICK HERE</button>
<h3>
Click on the above button to extract the emojis using regex
</h3>
<script>
   let sampleEle = document.querySelector(".sample").innerHTML;
   let btnEle = document.querySelector(".btn");
   let resEle = document.querySelector(".result");
   const EmojiRegEx = /\p{Emoji_Presentation}/gu;
   btnEle.addEventListener("click", () => {
      resEle.innerHTML = sampleEle.match(EmojiRegEx);
   });
</script>
</body>
</html>

輸出

以上程式碼將產生以下輸出 −

單擊“CLICK HERE”按鈕 −

更新於:15-7-2020

125 次瀏覽

開啟你的 事業

透過完成課程獲得認證

立即開始
廣告