簡體   English   中英

檢查字符串中的單詞是否與數組中的單詞匹配,如果匹配則將其從字符串中刪除

[英]Check if a word in a string matches a word in an array and if so remove it from the string

我想知道是否有人可以提供幫助,如果我想對字符串中的單詞是否與數組中的單詞匹配進行不區分大小寫的檢查,如果匹配,則從字符串中刪除該單詞什么是最好的方法?

非常感謝。

var testArray = new Array('that','from','again');

var testString = "It's That time again";

所以在這個例子中,單詞'That'和'again'將從字符串中刪除。

您可以拆分字符串以過濾字符串中的單詞。 最后加入他們以取回字符串:

 var testArray = new Array('that','from','again'); var testString = "It's That time again"; var resStr = testString.split(' ').filter(w =>.testArray.includes(w.toLowerCase()));join(' '). console;log(resStr);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM