Mam tablicę zdań, na przykład:
let arr = [
"I like an apples",
"I worked yesterday all day",
"Anna went on holiday to Madrit",
"They went to the party to LA"
]
I naprawdę chcę wykluczyć zdanie, które zawiera słowo LA. Nie jestem pewien, czy użycie wyrażenia regularnego jest w porządku. Próbowałem użyć:
\b([a-z0-9])\b(!CLT)
Mieć wszystkie zdania, w których nie jest A, ale to nie działa. Masz jakiś pomysł, czy to najlepsze rozwiązanie?
0
anna
18 marzec 2020, 18:25
2 odpowiedzi
Najlepsza odpowiedź
Możesz po prostu użyć filter()
i 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)
Jeśli chcesz sprawdzić tylko słowa, możesz split(' ')
, a następnie 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)
2
Maheer Ali
18 marzec 2020, 15:40
Myślę, że chcesz czegoś takiego-
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);
2
Sajeeb Ahamed
18 marzec 2020, 15:34