I'm trying to determine the characters between the last white space characer and the end of the string.
Input: "this and that"
Output: "that"
I have tried the regex below but it doesnt work!
var regex = /[\s]$/
Can 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+$
document.writeln("this and that".match(/\S+$/));
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.