3

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?

j08691
  • 204,283
  • 31
  • 260
  • 272
Charlie Stuart
  • 272
  • 2
  • 5
  • 19
  • 1
    Here's a duplicate, which itself is marked as a duplicate: http://stackoverflow.com/questions/23136691/replace-last-occurrence-word-in-javascript – Marc Aug 26 '15 at 13:54

4 Answers4

4

You'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");
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • That worked perfectly and you provided a great explanation, thanks! – Charlie Stuart Aug 26 '15 at 14:13
  • 1
    You might also want to use the case-insensitive modifier `/re/i` or '[Ss]' in case you get '123 main st'). Also, including a word boundary (as in some of the other answers) like `/\bSt.?\s*$/i`or `/\b[Ss]t.?\s*$/` will help prevent it from messing up addresses like '123 Amethyst' – John Hascall Jan 15 '17 at 21:52
  • @JohnHascall All true. I was focusing a *bit* too specifically in the original answer. Updated accordingly, thanks. – Paul Roub Jan 16 '17 at 14:51
1
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

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Arūnas Smaliukas
  • 3,231
  • 6
  • 27
  • 46
  • I tried this but for some reason it didn't replace my 'St' value. The value still appeared as 'Church St' – Charlie Stuart Aug 26 '15 at 14:07
  • http://jsfiddle.net/euy7hdr6/1/ - more examples (in comments what you should see in console). If it still doesn't fit your need, I can help to change it. Just give me a case witch doesn't work like you want. – Arūnas Smaliukas Aug 26 '15 at 14:18
0

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");
joyBlanks
  • 6,419
  • 1
  • 22
  • 47
0

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>
Gaël Barbin
  • 3,769
  • 3
  • 25
  • 52