Hi,
I need to remove the following format from the end of a string in javascript
1234, Australia
And only at the end of a string.
How would I do this?
Hi,
I need to remove the following format from the end of a string in javascript
1234, Australia
And only at the end of a string.
How would I do this?
Ok, so I found out what I was doing wrong...
var a = '888 Welles St, Scoresby Victoria 3179, Australia'.replace('/\d{4}, Australia/', '');
alert(a);
I was surrounding the regex pattern in quotes. Which it apparently doesn't need. So this works:
var a = '888 Welles St, Scoresby Victoria 3179, Australia'.replace(/\d{4}, Australia/, '');
alert(a);
Your solution is good.
I would add the $
so as not to replace anything unintentionally:
a = strVar.replace((/\d{4}, \w+$/,'');
Explanation from here:
/and$/ matches "and" in "land" but not "landing"
And you can even get a little more crazy by adding word boundaries:
a = strVar.replace((/\d{4}, \b\w+\b$/,'');