I need to split a string after a phrase for example id the string is "Stay in London" I need to remove "Stay in" I know this must be simple but I am finding it hard to find an approach to do this.
2 Answers
var str = "Stay in London",
//Here's a few ways, but there are many more
//and it all depends how dynamic it needs to be...
london = str.split(' ').pop(),
london = str.match(/\b(\w+)$/)[0],
london = str.slice(8),
london = str.replace('Stay in ', ''),
//and we could get silly... ;)
london = [].filter.call(str, function (letter, index) {
return index >= 8;
}).join(''),
london = str.split(' ').reduce(function (r, token) {
if (r.tokensToExclude.indexOf(token) == -1) r.finalString += token;
return r;
}, { tokensToExclude: ['Stay', 'in', ' '], finalString: '' }).finalString;
"Stay in London".split(" ").pop()