How do I add slashes to a string in Javascript?

133,574

Solution 1

replace works for the first quote, so you need a tiny regular expression:

str = str.replace(/'/g, "\\'");

Solution 2

Following JavaScript function handles ', ", \b, \t, \n, \f or \r equivalent of php function addslashes().

function addslashes(string) {
    return string.replace(/\\/g, '\\\\').
        replace(/\u0008/g, '\\b').
        replace(/\t/g, '\\t').
        replace(/\n/g, '\\n').
        replace(/\f/g, '\\f').
        replace(/\r/g, '\\r').
        replace(/'/g, '\\\'').
        replace(/"/g, '\\"');
}

Solution 3

A string can be escaped comprehensively and compactly using JSON.stringify. It is part of JavaScript as of ECMAScript 5 and supported by major newer browser versions.

str = JSON.stringify(String(str));
str = str.substring(1, str.length-1);

Using this approach, also special chars as the null byte, unicode characters and line breaks \r and \n are escaped properly in a relatively compact statement.

Solution 4

To be sure, you need to not only replace the single quotes, but as well the already escaped ones:

"first ' and \' second".replace(/'|\\'/g, "\\'")

Solution 5

An answer you didn't ask for that may be helpful, if you're doing the replacement in preparation for sending the string into alert() -- or anything else where a single quote character might trip you up.

str.replace("'",'\x27')

That will replace all single quotes with the hex code for single quote.

Share:
133,574
TIMEX
Author by

TIMEX

Updated on July 09, 2022

Comments

  • TIMEX
    TIMEX almost 2 years

    Just a string. Add \' to it every time there is a single quote.

  • Matthew Crumley
    Matthew Crumley over 14 years
    Those strings are identical. Unless you meant '\\x27'.
  • Kobi
    Kobi over 12 years
    @AlexeyLebedev - The second argument isn't a regex pattern - you only need one backslash in the string.
  • Alexey Lebedev
    Alexey Lebedev over 12 years
    let's see: str = "\\'"; str = str.replace(/'/g, "\\'");
  • Alexey Lebedev
    Alexey Lebedev over 12 years
    Yields a string with an escaped slash and unescaped quote. While your example is a correct answer to the question, it doesn't escape string properly.
  • Kobi
    Kobi over 12 years
    @Alexey - Haaa... That's what you meant. You're right then. It's easy to fix, if it's needed.
  • Kobi
    Kobi over 12 years
    Dear anonymous downvoter - I can only assume I didn't fulfill requirements that only exists in your head.
  • ram
    ram almost 10 years
    this replace results with two backslash added with single quote. :(
  • Kobi
    Kobi almost 10 years
    @ram - Try to alert the string, or print it using console.log. It should only have one backslash, but will be displayed escaped in some tools (the debugger, for example)
  • thelem
    thelem over 8 years
    Supported by all browsers with greater than 0.1% global usage share, although version 8 of IE must be in standards mode.
  • buycanna.io
    buycanna.io almost 7 years
    This function will do the opposite, hope this okay to post here...yikes.. String.prototype.stripSlashes = function(){ return this.replace(/\(.)/mg, "$1"); }