Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need is the last match. In the case below the word test without the $ signs or any other special character:

Test String:

$this$ $is$ $a$ $test$ 

Regex:

\b(\w+)\b
like image 861
ps0604 Avatar asked May 08 '15 14:05

ps0604


People also ask

What is end of string in RegEx?

End of String or Line: $ The $ anchor specifies that the preceding pattern must occur at the end of the input string, or before \n at the end of the input string. If you use $ with the RegexOptions. Multiline option, the match can also occur at the end of a line.

How do I find the end of a line in RegEx?

Under Search Mode: choose “Regular expression” and then check the “matches newline” checkbox. You should see closing </p> tags at the end of each line.

What does \b do in RegEx?

The \b metacharacter matches at the beginning or end of a word.

What is the i at the end of RegEx?

/i stands for ignore case in the given string. Usually referred to as case-insensitive as pointed out in the comment.


2 Answers

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*$
like image 67
six fingered man Avatar answered Oct 05 '22 14:10

six fingered man


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.

like image 22
User_coder Avatar answered Oct 05 '22 16:10

User_coder