I am trying to write a regex to get the last name of a person.
var name = "My Name";
var regExp = new RegExp("\s[a-z]||[A-Z]*");
var lastName = regExp(name);
Logger.log(lastName);
If I understand correctly \s
should find the white space between My
and Name
, [a-z]||[A-Z]
would get the next letter, then *
would get the rest. I would appreciate a tip if anyone could help out.
You can use the following regex:
But, from your requirements, it is simpler to just use
.split()
:Or
.substring()
(useful if there are more than one "last names"):