Is there is any way to remove a text from string and return the removed part, for example
var foo = "Hello world!";
var bar = foo.remove("world!");
console.log(foo); // "Hello "
console.log(bar); // "world!"
All I can think of is
var foo = "Hello world!";
var bar;
foo = foo.replace("world!",function(m){
bar = m;
return "";
});
But I hope there is a better way to achieve it