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
. How could I do that?
CodePudding user response:
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.
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);
}
CodePudding user response:
This pattern would work, but it would only work for first space
[^\s]*\s
Usage : str.replace(/[^\s]*\s/,"")
If you want to support something like "John Doe"
Then this pattern would work [^\s]*\s
console.log("John Doe".replace(/[^\s]*\s/,"")) //only one first space
console.log("John Doe".replace(/[^\s]*\s /,"")) //many spaces