Regex: I Want To Match Only Words Without A Dot At The End
For example: George R.R. Martin I want to match only George and Martin. I have tried: \w+\b. But doesn't work!
Solution 1:
The \w+\b.
matches 1+ word chars that are followed with a word boundary, and then any char that is a non-word char (as \b
restricts the following .
subpattern). Note that this way is not negating anything and you miss an important thing: a literal dot in the regex pattern must be escaped.
You may use a negative lookahead (?!\.)
:
var s = "George R.R. Martin";
console.log(s.match(/\b\w+\b(?!\.)/g));
See the regex demo
Details:
\b
- leading word boundary\w+
- 1+ word chars\b
- trailing word boundary(?!\.)
- there must be no.
after the last word char matched.
See more about how negative lookahead works here.
Post a Comment for "Regex: I Want To Match Only Words Without A Dot At The End"