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
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.
本文标签:
版权声明:本文标题:javascript - Regex to capture characters between the last white space character and the end of string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742328813a2454268.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论