How to extract the last word in a string with a JavaScript regex?

In some cases a word may be proceeded by non-word characters, for example, take the following sentence:

Marvelous Marvin Hagler was a very talented boxer!

If we want to match the word boxer all previous answers will not suffice due the fact we have an exclamation mark character proceeding the word. In order for us to ensure a successful capture the following expression will suffice and in addition take into account extraneous whitespace, newlines and any non-word character.

[a-zA-Z]+?(?=\s*?[^\w]*?$)

https://regex101.com/r/D3bRHW/1

We are informing upon the following:

  1. We are looking for letters only, either uppercase or lowercase.
  2. We will expand only as necessary.
  3. We leverage a positive lookahead.
  4. We exclude any word boundary.
  5. We expand that exclusion,
  6. We assert end of line.

The benefit here are that we do not need to assert any flags or word boundaries, it will take into account non-word characters and we do not need to reach for negate.


The $ represents the end of the string, so...

\b(\w+)$

However, your test string seems to have dollar sign delimiters, so if those are always there, then you can use that instead of \b.

\$(\w+)\$$

var s = "$this$ $is$ $a$ $test$";

document.body.textContent = /\$(\w+)\$$/.exec(s)[1];

If there could be trailing spaces, then add \s* before the end.

\$(\w+)\$\s*$

And finally, if there could be other non-word stuff at the end, then use \W* instead.

\b(\w+)\W*$