Reputation: 15
Ok so here is some REGEX:
/\w*a\w*/g
(with global flag)
This is to find any word that has an "a". (example: matches apple, ark and petal in this array P {ent, apple, peter, ark, petal}
Is there anyone who can think of a more efficient way to write this? Many thanks in advance, Alex
Upvotes: 0
Views: 44
Reputation: 177960
Or not use a regex, this is more readable
const letter = "a",
arr = ["ent", "apple", "peter", "ark", "petal"];
console.log(arr.filter(word=>word.includes(letter)))
Upvotes: 1
Reputation: 163362
You can exclude a from the word characters until you have found it using a negated character class where \W
matches any non word character.
[^\Wa]*a\w*
[^\Wa]*
Optionally repeat any word chars without aa\w*
Match a
and optional word charsUpvotes: 1