如何在 JavaScript 中執行全詞搜尋
使用 JavaScript 的 search()方法匹配全詞可以透過使用正規表示模式,用表示的單字邊界來界定所需的單字b.
範例實作:
const lookup = '\n\n\n\n\n\n2 PC Games \n\n\n\n'; const textbox = document.getElementById('textbox'); // Trim whitespace from 'lookup' for accurate matching lookup = lookup.trim(); // To perform a dynamic search based on 'lookup', utilize a dynamic regular expression const regex = new RegExp("\b" + lookup + "\b"); // Test the regex against the value of the 'textbox' if (regex.test(textbox.value)) { // Handle success case: word found as a whole word } else { // Handle failure case: word not found as a whole word }
附加說明:
以上是如何使用 JavaScript 只搜尋整個單字?的詳細內容。更多資訊請關注PHP中文網其他相關文章!