![](/img/trans.png)
[英]RegExp: I want to remove unnecessary words in the Sentence. How can I do it?
[英]How to exclude certain words in sentence. regex?
我有一系列句子,例如:
let arr = [
"I like an apples",
"I worked yesterday all day",
"Anna went on holiday to Madrit",
"They went to the party to LA"
]
我真的想排除一個句子,它有一個 LA 詞。 我不確定使用正則表達式是否可以。 我試過使用:
\b([a-z0-9])\b(!CLT)
讓所有句子都不是洛杉磯,但它不起作用。 知道這是解決這個問題的最佳解決方案嗎?
您可以簡單地使用filter()
和includes()
let arr = [ "I like an apples", "I worked yesterday all day", "Anna went on holiday to Madrit", "They went to the party to LA" ] const res = arr.filter(x => !x.includes('LA')); console.log(res)
如果你只想檢查單詞,那么你可以split(' ')
然后filter()
let arr = [ "I like an apples", "I worked yesterday all day", "Anna went on holiday to Madrit", "They went to the party to LA", "LAST Four words" ] const res = arr.filter(x => !x.split(' ').includes('LA')); console.log(res)
我想你想要這樣的東西-
let arr = [ "I like an apples", "I worked yesterday all day", "Anna went on holiday to Madrit", "They went to the party to LA", "Hello world LA Demo" ]; let modifiedArr = arr.filter(v => { let regex = /\\bLA\\b/i; if (!regex.test(v)) { return true; } return false; }); console.log(modifiedArr);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.