![](/img/trans.png)
[英]Check if a word in a string matches a word in an array and if so remove it from the string
[英]Remove word from string if included in array?
我認為這是一個相對直截了當的問題,但是我在解決問題時遇到了一些問題。
我有一個字符串數組和一個句子。 如果數組中的任何單詞出現在句子中,請將它們刪除。
const products = ["premium t-shirt", "t-shirt", "sweatshirt", "baseball tee", "v-neck t-shirt", "long sleeve t-shirt", "raglan baseball tee", "pullover hoodie", "tank top", "zip hoodie",
"hoodie", "shirt", "tee", "tshirt"];
const name = "I'd Rather Be Beekeeping Save The Bees Honey Bee Beehive Zip Hoodie T-Shirt".toLowerCase();
let strippedName;
products.forEach(p => {
if(name.includes(p)) strippedName = name.replace(p, "");
});
以上刪除了 shirt 一詞,但沒有刪除t-shirt
。 還有一個問題是它需要在 strippedName 變量上循環而不是命名。
我不確定是否有更好的方法來做到這一點?
您每次都使用name
字符串中的最新替換值覆蓋 strippedName,而在下一個值上使用替換時,您需要傳遞使用最后替換的值
const products = ["premium t-shirt", "t-shirt", "sweatshirt", "baseball tee", "v-neck t-shirt", "long sleeve t-shirt", "raglan baseball tee", "pullover hoodie", "tank top", "zip hoodie","hoodie", "shirt", "tee", "tshirt"]; const name = "I'd Rather Be Beekeeping Save The Bees Honey Bee Beehive Zip Hoodie T-Shirt".toLowerCase(); let strippedName = name; products.forEach(p => { if (name.includes(p)){ strippedName = strippedName.replace(p, ""); } }); console.log(strippedName)
將數組轉換為不區分大小寫的全局正則表達式。
const products = ["premium t-shirt", "t-shirt", "sweatshirt", "baseball tee", "v-neck t-shirt", "long sleeve t-shirt", "raglan baseball tee", "pullover hoodie", "tank top", "zip hoodie", "hoodie", "shirt", "tee", "tshirt" ]; const name = "I'd Rather Be Beekeeping Save The Bees Honey Bee Beehive Zip Hoodie T-Shirt"; let strippedName = name.replace(new RegExp(products.join("|"), "gi"), ""); console.log(strippedName);
products
字符是正則表達式特殊字符,則首先需要對其進行轉義。
我想你的意思是:
const products = ["premium t-shirt", "t-shirt", "sweatshirt", "baseball tee", "v-neck t-shirt", "long sleeve t-shirt", "raglan baseball tee", "pullover hoodie", "tank top", "zip hoodie",
"hoodie", "shirt", "tee", "tshirt"];
var name = "I'd Rather Be Beekeeping Save The Bees Honey Bee Beehive Zip Hoodie T-Shirt".toLowerCase();
let strippedName = name;
products.forEach(p => {
if(name.includes(p)){
strippedName = strippedName.replace(p, "");
}
});
const products = ["premium t-shirt", "t-shirt", "sweatshirt", "baseball tee", "v-neck t-shirt", "long sleeve t-shirt", "raglan baseball tee", "pullover hoodie", "tank top", "zip hoodie",
"hoodie", "shirt", "tee", "tshirt"];
const name = "I'd Rather Be Beekeeping Save The Bees Honey Bee Beehive Zip Hoodie T-Shirt".toLowerCase();
let strippedName = name;
products.forEach(p => {
if(strippedName.includes(p)) {
strippedName = strippedName.replace(p, "");
}
});
console.log(strippedName);
包含方法區分大小寫。 它將以不同的方式考慮“T 恤”和“T 恤”。 您應該考慮將字符串轉換為大寫/小寫,然后執行包含方法。
const products = [“高級 T 恤”、“T 恤”、“運動衫”、“棒球 T 恤”、“V 領 T 恤”、“長袖 T 恤”、“插肩棒球 T 恤”、“套頭連帽衫”、“背心”、“拉鏈連帽衫”、“連帽衫”、“襯衫”、“T恤”、“T恤”];
const name = "I'd Rather Be Beekeeping Save The Bees Honey Bee Beehive Zip Hoodie T-Shirt".
const n = name.toUpperCase();
let strippedName;
products.forEach(p => {
if(n.includes(p.toUpperCase())) strippedName = new.replace(p, "");
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.