1

I am trying to replace the & in "Sydney & Melbourne" with &. But it's not working.

I have tried a few different ways as follows:

  • with for loop and if statement:

    for(var i=0; i<str.length; i++){
        if(str[i]==="&"){
          str[i]="&amp;";
        }
    
  • with regex and replace:

    var myRegExp = /\b&\b/;
    
    str = str.replace(myRegExp,"&amp;");
    return str;
    

I do understand that & and &amp; are the same things and so the result probably comes out at as & (in fact it's happening as I am writing it here on stack overflow). But is there a way around it?

7
  • 1
    Remove \b from regex. 'Sydney & Melbourne'.replace(/&/g, '&amp;'); Commented Jan 13, 2017 at 5:31
  • tried that but it didn't work:( Commented Jan 13, 2017 at 5:32
  • Thanks! I stand corrected, it did work. just curious as to why single quotes worked but the double quotes didn't. Commented Jan 13, 2017 at 5:40
  • It is impossible that replace(/&/g, '&amp;)` would not work, whether with double quotes or single quotes. Commented Jan 13, 2017 at 5:40
  • 1
    Maybe your double quotes were accidentally some kind of "smart quotes", that were themselves breaking things. That's why changing the quotes seemed to fix the problem. (just a thought.) Commented Jan 13, 2017 at 5:53

2 Answers 2

3

Your first try

with for loop and if statement:

for(var i=0; i<str.length; i++){
    if(str[i]==="&"){
      str[i]="&amp;";
    }
}

Of course this won't work. JS strings are immutable, which means

Once a string is created, it is not possible to modify it

It won't cause a run-time error, but it won't do anything. (Even if JS strings were not immutable, which they are, you could not replace one character with multiple characters.)

Your second try

with regex and replace:

var myRegExp = /\b&\b/;

str = str.replace(myRegExp,"&amp;");
return str;

Of course this won't work. There is no word boundary between a space and an ampersand. See the definition of word boundary:

A word boundary matches the position where a word character is not followed or preceded by another word-character.

where "word character" is equivalent to [a-zA-Z0-9_].

But why?

However, the real question is why you want to do this. If you simply want to insert this string into the DOM as text, then do so using textContent:

document.getElementById("city").textContent = "Sydney & Melbourne";

(instead of using innerHTML). In jQuery, if you happen to be using that, use text() instead of html(). This approach has the advantage that it won't be confused by other HTML characters in the string, notably <.

If your issue is related to & in a URL, you shouldn't be HTML-escaping it--you should be URI-encoding it, but you probably already knew that.

If your issue is that you are passing this to a server which expects properly encoded HTML strings, then you should reconsider your API design. In general, it's better to store the raw strings on the server, and decode/encode/escape/unescape them when necessary--remember that server data might be displayed in contexts other than browser. If you absolutely do want to send the server properly HTML-escaped strings, then you need to worry about more than just &, but also the other special HTML characters. For this, you should use some utility that is probably available in your favorite library, or the standard:

function htmlEscape(str) {
  var div = document.createElement('div');
  div.textContent = str;
  return div.innerHTML;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Currently, in order to replace the &, you would need to have the & surrounded by word characters (ex: a&b).

Here is a JSFiddle that explains visually what I mean about the regex you were trying to use.

Instead, just target the & directly:

var string = 'Sydney & Melbourne';

string = string.replace(/&/g, '&amp;');

console.log(string);

4 Comments

Thank you both! @kevBot - The single quotes worked but the double quotes didn't work?? why is this?
@nishkaush, It works fine for me with double quotes. Are you referring to when you were trying to use the word boundaries (\b)?
Hi kevbot @kevbot - i initially started without /b but then i thought maybe "&" has spaces before and after, so i ought to add /b. Its so strange that this didn't work when everyone here is saying it should.
If you see @torazaburo's answer, he details a little more of "why" it didn't work. I figured I'd point you there since he wrote up the "why" before I did

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.