2

For example: George R.R. Martin I want to match only George and Martin.

I have tried: \w+\b. But doesn't work!

1 Answers1

4

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.

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    That's all I need. Thank you, so much! I will mark that answer as the best in few mins. –  Oct 16 '16 at 20:59