简体   繁体   中英

Regex to capture characters between the last white space character and the end of string

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]$/

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+$

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM