Y Anderson
Y Anderson

Reputation: 445

Javascript Regex expression for matching words with digits ignoring 4 digit numbers

I have a string that is just words with a single whitespace in-between. Assuming no special characters I would like to match all words containing digits while ignoring 4 digit numbers.

I.E.

hello12345 12345hello 123456789 12 red hello 1234 5678

Would Match:

hello12345 12345hello 123456789 12

The ultimate goal would be replacing hello12345 12345hello 123456789 12 with an empty string resulting in:

red hello 1234 5678

The following \w*\d\w* matches words with digits and \b\d{4}\b matches all 4 digit numbers. However, I am unsure of how to combine them.

Upvotes: 1

Views: 165

Answers (2)

marsze
marsze

Reputation: 17035

This regex

/((\b(\d{1,3}|\d{5,})\b)|([a-z]+\d\w*|\w*\d[a-z]+))\s*/gi

matches:

// Digit-only words with less than or more than 4 digits
\b(\d{1,3}|\d{5,})\b

// Words that contain at least a number and a letter
[a-z]+\d\w*|\w*\d[a-z]+

incl. whitespace between them.

var string = "hello12345 12345hello 123456789 12 red hello 1234 5678";
var regex = /((\b(\d{1,3}|\d{5,})\b)|([a-z]+\d\w*|\w*\d[a-z]+))\s*/gi
console.log(string.replace(regex, ""));

There might be a simpler one. That's just from the top of my head.

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

Match and capture what you need and just match what you do not need (see The Best Regex Trick Ever):

var re = /\b\d{4}\b|(\w*\d\w*)/g;
var str = "hello12345 12345hello 123456789 12 red hello 1234 5678";
var m, res = [];
while (m = re.exec(str)) {
    if (m[1]) res.push(m[1]);
}
console.log(res);

The \b\d{4}\b alternative is only matched, but the second one, (\w*\d\w*), is also captured with the help of the capturing group, (...). This value is kept in Group 1, accessed via m[1].

Upvotes: 1

Related Questions