Im using an API call to populate some fields on my website. These fields are populated with different parts of an address. However, in my first address line field the value is abbreviated. For example, if i had 'Smith Street' it would get inserted as 'Smith St'. To get around this issue i am using javascript to replace the value, for example:
value = value.replace("St", "Street");
However if i then have, for example, a value that is 'Stanley Street' it would return 'Streetanley Street'.
Does anybody know of a method i can use to apply the replace method to the last word in a string?
Im using an API call to populate some fields on my website. These fields are populated with different parts of an address. However, in my first address line field the value is abbreviated. For example, if i had 'Smith Street' it would get inserted as 'Smith St'. To get around this issue i am using javascript to replace the value, for example:
value = value.replace("St", "Street");
However if i then have, for example, a value that is 'Stanley Street' it would return 'Streetanley Street'.
Does anybody know of a method i can use to apply the replace method to the last word in a string?
Share Improve this question edited Aug 26, 2015 at 13:54 j08691 208k32 gold badges269 silver badges280 bronze badges asked Aug 26, 2015 at 13:51 Charlie StuartCharlie Stuart 2822 gold badges5 silver badges21 bronze badges 1- 1 Here's a duplicate, which itself is marked as a duplicate: stackoverflow./questions/23136691/… – Marc Commented Aug 26, 2015 at 13:54
4 Answers
Reset to default 5You're looking for a regular expression. Get used to them, if you plan on writing much JavaScript.
value = value.replace(/St$/, "Street");
will replace "St"
only if it's the end of the string. ($
matches end-of-string)
If we wanted to allow for white space at the end of the string, and still replace, we would say:
value = value.replace(/St\s*$/, "Street");
Where \s
means "any white space character" and *
means "0 or more times".
And if we want to match both "St"
and "St."
, we'd say:
value = value.replace(/St\.?\s*$/, "Street");
where \.
is just a ".", and ?
means "at most once".
To avoid replacing "st" in the middle of a word, use a word boundary (\b
):
value = value.replace(/\bSt\.?\s*$/, "Street");
And you probably want to use a case-insensitive match (/i
), so "Main st" is converted just as well as "Main Street":
value = value.replace(/\bSt\.?\s*$/i, "Street");
value = value.replace(/(\s)St(\S*)$/, "$1Street$2");
/\sSt\S*$/
will match last word if it is beggining with St
(\s
- whitespace character, then goes St
and then \S
- not whitespace character *
many times, and then goes $
- end of string).
Then you need to wrap with ()
any parts you will need to re-use and then re-use them with $1
$2
etc
You can use a boundary across your word. To create a boundary wrap your word in
/\bYourWord or words\b/g
value.replace(/\bSt\b/g, "Street");
You can use the word boundary expression: \b
var abbreviations= {
"st":"street",
"av":"avenue"
//...
};
for( var i in abbreviations ){
str= str.replace( new RegExp( "\\b" + i + "\\b" ,"i" ) , abbreviations[i] );
}
document.querySelector("input").addEventListener("input",function(evt){
document.querySelector("#output").innerHTML= correctAbbreviations(evt.target.value);
});
function correctAbbreviations(str){
var abbreviations= {
"st":"street",
"av":"avenue"
//...
};
for( var i in abbreviations ){
str= str.replace( new RegExp( "\\b" + i + "\\b" ,"i" ) , abbreviations[i] );
}
return str;
};
#output{
background:#ddd;
width:auto;
}
<input type="text" >
<br/><span id="output"></span>