15

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
5
  • 2
    Or use .split on the string instead and grab the last element with .pop. Much simpler. Commented May 8, 2015 at 14:27
  • 1
    Use input.match(/\b\w+\b/g).pop(); Commented May 8, 2015 at 14:28
  • @Andy I noticed your comment after adding my answer and I agree. Regexes should be avoided wherever possible. If you put this in an answer you'll at least get a +1 from me :) Commented May 8, 2015 at 14:58
  • @Andy and @Jonathan Mee, how to tell split to use special characters in addition to space? Commented May 8, 2015 at 15:02
  • @pgschr I'll deffer to Andy on this, but the simplest method would be to split on space, take your last element and just chop the first and last character off it. Commented May 8, 2015 at 15:09

5 Answers 5

33

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*$
Sign up to request clarification or add additional context in comments.

4 Comments

I think the only valid answer is your last regex. The first three does not meet the confirmation criteria.
@GarisMSuero: I don't know what the actual string may be. If the test string is representative of the actual string format then all but the first should work. If he's using $ as a weird way to represent the word boundaries, then the first one would work. The question isn't entirely clear, so I gave options for each.
yeah, agree but he also stated: " without the $ signs or any other special character, how can this be achieved?"
@GarisMSuero: Yes, you're right. The last one would be it then.
4

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.

Comments

0

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

If you use var result = input.match("\b(\w+)\b") an array of all the matches will be returned next you can get it by using pop() on the result or by doing: result[result.length]

2 Comments

Why would I write a regexp which returns all the matches and then choose the last one, instead of just writing a regexp which chose the last one to begin with?
I didn't know that was even possible, will look into it
0

Your regex will find a word, and since regexes operate left to right it will find the first word.

A \w+ matches as many consecutive alphanumeric character as it can, but it must match at least 1.
A \b matches an alphanumeric character next to a non-alphanumeric character. In your case this matches the '$' characters.

What you need is to anchor your regex to the end of the input which is denoted in a regex by the $ character.

To support an input that may have more than just a '$' character at the end of the line, spaces or a period for instance, you can use \W+ which matches as many non-alphanumeric characters as it can:

\$(\w+)\W+$

Comments

0

Avoid regex - use .split and .pop the result. Use .replace to remove the special characters:

var match = str.split(' ').pop().replace(/[^\w\s]/gi, '');

DEMO

1 Comment

this solution does not consider special characters in addition to space

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.