Divide a string using JavaScript regular expressions
I have a string like
"Employee Name is Jason Taylo开发者_运维百科r"
I need a regular expression to extract what is in the left hand side of "is"
"Employee Name"
And another to extract what is in the right hand side of "is"
"Jason Taylor"
I have this
function BreakString(string) {
return string.replace(/((.+?)(is)).*/, '$1');
}
which returns "Employee Name is"
Can you please help me to solve this?
You missed a backreference:
function BreakString(string) {
return string.replace(/(.+?) is (.*)/, '$1 $2'));
}
or without regex
function noIs(str) {
var parts = str.split(" is ");
return parts[0] + ":" + parts[1];
}
([^.]+) is ([^.]+)
seems to work
精彩评论