Jefferson Reis
Jefferson Reis

Reputation: 88

Regex to match entire words containing only specific characters

I need to find all the words that have the specific characters(letters, numbers and special characters) and ignore the rest.

Here's my expression:

/[cwh]\w+/ig

Example:

The expression need match case insensitive.

Thank you very much for your help.

Upvotes: 3

Views: 2040

Answers (2)

revo
revo

Reputation: 48711

You have to use positive lookaheads:

\b(?=\w*c)(?=\w*h)(?=\w*w)\w+

Live demo

Upvotes: 4

Deepak
Deepak

Reputation: 22

I'm not sure exactly what your match rules are - since both watch (lowercase w) and Welcome (lowercase c) would match the characters in your list. But

\w*[cwh]\w*/g

is probably what you're after.

This will match zero or more word characters, one character from your list, and zero or more word characters in a case sensitive fashion (w is distinct from W) globally (multiple matches possible per line).

Upvotes: 0

Related Questions