Sunday 15 January 2012

javascript - match regex - How to get the difference between the two strings when matched -



javascript - match regex - How to get the difference between the two strings when matched -

i perform test if string contains substring, if yes, have substring not matched

var string = "hello world"; var tomatch = new regexp('world', 'i'); if (string.match(tomatch)){ // should homecoming "hello " }

just utilize replace():

var string = "hello world"; var tomatch = new regexp('world', 'i'); var newstring=string.replace(string.match(tomatch),"");

if want return, use

return string.replace(string.match(tomatch),"");

it give string without "world" if contained "world" , unmodified string if there no "world"

javascript

No comments:

Post a Comment