I have this line of code and the \ is generating problems (at least in the highlighting of Sublime Text. How Can I escape this character?
text = text.replace(":\","<img src='images/Smilleys_08.png' class='smilley' />");
text = text.replace(":-\","<img src='images/Smilleys_08.png' class='smilley' />");
Also the same code needs to be in PHP and it goes like this
$tempText = str_replace(":\","<img src='images/Smilleys_08.png' class='smilley' />",$tempText);
$tempText = str_replace(":-\","<img src='images/Smilleys_08.png' class='smilley' />",$tempText);
Just like you would escape any other character: Add a \
in front of it:
text = text.replace(":\\","<img src='images/Smilleys_08.png' class='smilley' />");
This counts for both php as JavaScript.
Working example:
alert(":\\ bla bla :\\ test".replace(":\\","<img src='images/Smilleys_08.png' class='smilley' />"));
Just keep in mind that a string replace only replaces the first occurrence of the search string, as shown in the demo.
To fix that, try this regex replace:
alert(":\\ bla bla :\\ test".replace(/:\\/g,"<img src='images/Smilleys_08.png' class='smilley' />"));
</div>