I'm trying to determine the characters between the last white space characer and the end of the string.
Example
Input: "this and that"
Output: "that"
I have tried the regex below but it doesnt work!
var regex = /[\s]$/
I'm trying to determine the characters between the last white space characer and the end of the string.
Example
Input: "this and that"
Output: "that"
I have tried the regex below but it doesnt work!
var regex = /[\s]$/
Share
Improve this question
edited Jun 20, 2020 at 9:12
CommunityBot
11 silver badge
asked Oct 27, 2012 at 17:43
boomboom
11.7k9 gold badges47 silver badges66 bronze badges
0
4 Answers
Reset to default 7Can do without regex
var result = string.substring(string.lastIndexOf(" ")+1);
Using regex
result = string.match(/\s[a-z]+$/i)[0].trim();
I suggest you to use simple regex pattern
\S+$
Javascript test code:
document.writeln("this and that".match(/\S+$/));
Output:
that
Test it here.
You could just remove everything up to the last space.
s.replace(/.* /, '')
Or, to match any white space...
s.replace(/.*\s/, '')
Your example matches just one space character at the end of the string. Use
/\s\S+$/
to match any number.