[英]Javascript Text Variations Search
除了使用OR(||)表達式查找使用JavaScript的PO Box的所有變體之外,還有一種簡便的方法嗎?
if((Address).indexOf("PO BOX") > -1 || if((Address).indexOf("PO Box") > -1
地址字段是從預填充的數據庫列中提取的。 如果地址在地址欄中有郵政信箱的任何變化,我需要禁用一個選項。
我查看了Mozilla文檔,但沒有找到我可以使用的任何內容。
最好的可能是這樣的:
const found = Address.toLowerCase().includes("po box");
將地址轉換為小寫並搜索值
if((Address.toLowerCase()).indexOf("po box") > -1
您可以使用以下任何一種方式:
if( Address.test(/po box/gi) )
。 if( Address.toLowerCase().includes("po box"))
if( Address.toLowerCase().indexOf("po box")) > -1
PS:我個人認為第一種方法更干凈,更快捷。
也許只是將地址轉換為小寫?
const needle = "Po BOx"
if(Address.toLowerCase().indexOf(needle.toLowerCase()) > -1)
要么
if(Address.toLowerCase().includes(needle.toLowerCase()))
對於正則表達式而言,這將是一個合適的用例。 以下是一些測試-您可以看到test
函數返回一個布爾值,指示是否找到了搜索字符串。
第二個/
之后的i
表示正則表達式搜索不區分大小寫 。
const regex = /po box/i; const s1 = "Send to PO Box 1000"; const s2 = "Send to PO box 1000"; const s3 = "Send to po Box 1000"; const s4 = "Send to po box 1000"; const s5 = "Send somewhere else!"; console.log(regex.test(s1)); console.log(regex.test(s2)); console.log(regex.test(s3)); console.log(regex.test(s4)); console.log(regex.test(s5));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.