admin管理员组

文章数量:1333397

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
Add a ment  | 

4 Answers 4

Reset to default 7

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.

本文标签: