Reputation: 11
How can I match all words except if they contain a dot, like
I want to match everything except.this and similar words with dots in them
I tried \b(?!\w+\.\w+)\w+\b
but this didn't work.
No matter how I use \w+
, \\.
, etc. the regex engine still matches the part "ignore.me" behind the dot. Is there a simple syntax for it? Just escaping the dot doesn't seem to work.
Upvotes: 0
Views: 108
Reputation: 19000
I suggest the following pattern:
(?:^|\s)(?:(?!\.)[\w'])+(?=\s|$|[.?!](?:\s|$))
JS/Regex test:
const regex = /(?:^|\s)(?:(?!\.)[\w'])+(?=\s|$|[.?!](?:\s|$))/g;
const str = `aaa blabla fasdfdsa ignoremenot.
bbb igno.reme ad
It's fine?`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match.trim()}`);
});
}
There is a catch: you have to trim the matches to remove unnecessary whitespaces that can show up since we cannot use lookbehind in JavaScript's regex, like this: (?<=^|\s)(?:(?!\.)[\w'])+(?=\s|$|[.?!](?:\s|$))
Upvotes: 1