如何在 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中文网其他相关文章!