Skip to content

Instantly share code, notes, and snippets.

@getify
Last active June 25, 2022 16:26
Show Gist options
  • Save getify/3667624 to your computer and use it in GitHub Desktop.
Save getify/3667624 to your computer and use it in GitHub Desktop.

Revisions

  1. getify revised this gist Dec 24, 2020. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -3,15 +3,15 @@ function escapeDoubleQuotes(str) {
    return str.replace(/\\([\s\S])|(")/g,"\\$1$2"); // thanks @slevithan!
    }

    escapeDoubleQuotes("ab"); // ab => ab
    escapeDoubleQuotes("a\"b"); // a"b => a\"b
    escapeDoubleQuotes("a\\\"b"); // a\"b => a\"b
    escapeDoubleQuotes("a\\\\\"b"); // a\\"b => a\\\"b
    escapeDoubleQuotes("a\\\\\\\"b"); // a\\\"b => a\\\"b
    escapeDoubleQuotes(`ab`); // ab => ab (nothing escaped)
    escapeDoubleQuotes(`a"b`); // a"b => a\"b
    escapeDoubleQuotes(`a\\"b`); // a\"b => a\"b (nothing escaped)
    escapeDoubleQuotes(`a\\\\"b`); // a\\"b => a\\\"b
    escapeDoubleQuotes(`a\\\\\\"b`); // a\\\"b => a\\\"b (nothing escaped)

    escapeDoubleQuotes("a\"b\"c"); // a"b"c => a\"b\"c
    escapeDoubleQuotes("a\"\"b"); // a""b => a\"\"b
    escapeDoubleQuotes("\"\""); // "" => \"\"
    escapeDoubleQuotes(`a"b"c`); // a"b"c => a\"b\"c
    escapeDoubleQuotes(`a""b`); // a""b => a\"\"b
    escapeDoubleQuotes(`""`); // "" => \"\"

    // don't unnecessarily escape:
    escapeDoubleQuotes(escapeDoubleQuotes(escapeDoubleQuotes("a\"b"))); // a"b => a\"b
    escapeDoubleQuotes(escapeDoubleQuotes(escapeDoubleQuotes(`a"b`))); // a"b => a\"b
  2. getify revised this gist Sep 7, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,8 @@ escapeDoubleQuotes("a\\\\\"b"); // a\\"b => a\\\"b
    escapeDoubleQuotes("a\\\\\\\"b"); // a\\\"b => a\\\"b

    escapeDoubleQuotes("a\"b\"c"); // a"b"c => a\"b\"c
    escapeDoubleQuotes("a\"\"b"); // a""b => a\"\"b
    escapeDoubleQuotes("\"\""); // "" => \"\"

    // don't unnecessarily escape:
    escapeDoubleQuotes(escapeDoubleQuotes(escapeDoubleQuotes("a\"b"))); // a"b => a\"b
  3. getify revised this gist Sep 7, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    // NOTE: only escapes a " if it's not already escaped
    function escapeDoubleQuotes(str) {
    return str.replace(/(^|[^\\])((?:\\\\)*)\"/g,"$1$2\\\"");
    return str.replace(/\\([\s\S])|(")/g,"\\$1$2"); // thanks @slevithan!
    }

    escapeDoubleQuotes("ab"); // ab => ab
    @@ -9,5 +9,7 @@ escapeDoubleQuotes("a\\\"b"); // a\"b => a\"b
    escapeDoubleQuotes("a\\\\\"b"); // a\\"b => a\\\"b
    escapeDoubleQuotes("a\\\\\\\"b"); // a\\\"b => a\\\"b

    escapeDoubleQuotes("a\"b\"c"); // a"b"c => a\"b\"c

    // don't unnecessarily escape:
    escapeDoubleQuotes(escapeDoubleQuotes(escapeDoubleQuotes("a\"b"))); // a"b => a\"b
  4. getify revised this gist Sep 7, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -10,5 +10,4 @@ escapeDoubleQuotes("a\\\\\"b"); // a\\"b => a\\\"b
    escapeDoubleQuotes("a\\\\\\\"b"); // a\\\"b => a\\\"b

    // don't unnecessarily escape:

    escapeDoubleQuotes(escapeDoubleQuotes(escapeDoubleQuotes("a\"b"))); // a"b => a\"b
  5. getify revised this gist Sep 7, 2012. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,8 @@ escapeDoubleQuotes("ab"); // ab => ab
    escapeDoubleQuotes("a\"b"); // a"b => a\"b
    escapeDoubleQuotes("a\\\"b"); // a\"b => a\"b
    escapeDoubleQuotes("a\\\\\"b"); // a\\"b => a\\\"b
    escapeDoubleQuotes("a\\\\\\\"b"); // a\\\"b => a\\\"b
    escapeDoubleQuotes("a\\\\\\\"b"); // a\\\"b => a\\\"b

    // don't unnecessarily escape:

    escapeDoubleQuotes(escapeDoubleQuotes(escapeDoubleQuotes("a\"b"))); // a"b => a\"b
  6. getify revised this gist Sep 7, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // NOTE: only escapes a " if it's not already escaped
    function escapeDoubleQuotes(str) {
    return str.replace(/(^|[^\\])((?:\\\\)*)\"/g,"$1$2\\\"");
    }
  7. getify revised this gist Sep 7, 2012. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,8 @@ function escapeDoubleQuotes(str) {
    return str.replace(/(^|[^\\])((?:\\\\)*)\"/g,"$1$2\\\"");
    }

    escapeDoubleQuotes("ab"); // ab
    escapeDoubleQuotes("a\"b"); // a\"b
    escapeDoubleQuotes("a\\\"b"); // a\"b
    escapeDoubleQuotes("a\\\\\"b"); // a\\\"b
    escapeDoubleQuotes("a\\\\\\\"b"); // a\\\"b
    escapeDoubleQuotes("ab"); // ab => ab
    escapeDoubleQuotes("a\"b"); // a"b => a\"b
    escapeDoubleQuotes("a\\\"b"); // a\"b => a\"b
    escapeDoubleQuotes("a\\\\\"b"); // a\\"b => a\\\"b
    escapeDoubleQuotes("a\\\\\\\"b"); // a\\\"b => a\\\"b
  8. getify created this gist Sep 7, 2012.
    9 changes: 9 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    function escapeDoubleQuotes(str) {
    return str.replace(/(^|[^\\])((?:\\\\)*)\"/g,"$1$2\\\"");
    }

    escapeDoubleQuotes("ab"); // ab
    escapeDoubleQuotes("a\"b"); // a\"b
    escapeDoubleQuotes("a\\\"b"); // a\"b
    escapeDoubleQuotes("a\\\\\"b"); // a\\\"b
    escapeDoubleQuotes("a\\\\\\\"b"); // a\\\"b