[英]Javascript Regex everything before a space and including the space
I need to remove all text before a space and the space itself from a list of strings.我需要从字符串列表中删除空格之前的所有文本和空格本身。 For example, John Doe
needs to be just Doe
.例如, John Doe
需要只是Doe
。 How could I do that?我怎么能那样做?
Does it have to be regex?它必须是正则表达式吗? As an option;作为一种选择; if you need everything after the space, you can just use split
and then pop
to get the last item.如果您需要空格后的所有内容,您可以使用split
然后pop
来获取最后一项。
var str = 'John Doe';
var final = str.split(' ').pop();
console.log(final);
Here is a solution using a list of string, as you mentioned:正如您提到的,这是一个使用字符串列表的解决方案:
var strList = ['John Doe', 'Jane Smith', 'Stack Overflow']; for (var i = 0; i < strList.length; i++) { var final = strList[i].split(' ').pop(); console.log(final); }
This pattern would work, but it would only work for first space这种模式可行,但只适用于第一个空格
[^\s]*\s
Usage: str.replace(/[^\s]*\s/,"")
用法: str.replace(/[^\s]*\s/,"")
If you want to support something like "John Doe"
Then this pattern would work [^\s]*\s+
如果你想支持像"John Doe"
这样的东西那么这个模式就可以了[^\s]*\s+
console.log("John Doe".replace(/[^\s]*\s/,"")) //only one first space console.log("John Doe".replace(/[^\s]*\s+/,"")) //many spaces
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.