Skip to content
Advertisement

Regex expression for words with length of even number

I want to write a regex expression for words with even-numbered length.

For example, the output I want from the list containing the words:
{"blue", "ah", "sky", "wow", "neat"} is {"blue", "ah", "neat}.

I know that the expression w{2} or w{4} would produce 2-worded or 4-worded words, but what I want is something that could work for all even numbers. I tried using w{%2==0} but it doesn’t work.

Advertisement

Answer

You can repeat 2 word characters as a group between anchors ^ to assert the start and $ to assert the end of the string, or between word boundaries b

JavaScript

See a regex demo.

JavaScript

Output

JavaScript
Advertisement