Escaping double quotes in JavaScript from a string in a variable

Andres SK

I have these two scenarios:


console.log(('hello "friend" what\'s up?').replace(/\"/g, '\\"'));

I receive the expected result:

hello "friend" what's up?


But, if I do this:

var val = 'hello "friend" what\'s up?';
val.replace(/\"/g, '\\"');
console.log(val);

I get...

hello "friend" what's up?

(the result needs to be hello \"friend\" what's up?)


The only difference is that the second one uses an already created variable that contains the string. Why doesn't the second scenario actually replace the double quotes with \"?

Anthony Grist

From the MDN documentation:

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement.

You need to do val = val.replace(/\"/g, '\\"'); so that you're assigning the new string returned by calling replace to your variable.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Escaping quotes and double quotes

From Dev

Escaping double double quotes in html attributes in Javascript

From Dev

How to remove double quotes from default string in javascript/Jquery

From Dev

Removing the double quotes from a string

From Dev

Avoiding escaping double quotes in a string

From Dev

Escaping double quotes around argument of onClick in single quote string

From Dev

Escaping double quotes in CSS selector

From Dev

Escaping double quotes in a variable in a shell script

From Dev

double quotes escaping in golang exec

From Dev

External table in HIVE - Escaping double quotes from original data set

From Dev

Perl: Escaping the double-quotes expanded from a variable

From Dev

Java - escaping double quotes in string from file

From Dev

Passing a variable with escaping double quotes over ssh

From Dev

XSLT 1.0 escaping double quotes and backslash in a string

From Dev

Pass a string with double and single quotes from asp to javascript function

From Dev

Escaping a string with quotes in Laravel

From Dev

replace single quotes and double quotes from a string

From Dev

Passing an array to a function and escaping double quotes in Javascript

From Dev

Escaping double quotes around argument of onClick in single quote string

From Dev

Escaping double quotes in a variable in a shell script

From Dev

External table in HIVE - Escaping double quotes from original data set

From Dev

Escaping in double quotes in bash scripts

From Dev

Escaping quotes in PHP with variable

From Dev

Prevent LOAD DATA INFILE from escaping double double quotes

From Dev

Escaping double quotes fix

From Dev

Escaping double quotes in perl in Linux

From Dev

Escaping double quotes in .csv

From Dev

Javascript - double quotes in variable

From Dev

How to match a string in double quotes with escaping?

Related Related

  1. 1

    Escaping quotes and double quotes

  2. 2

    Escaping double double quotes in html attributes in Javascript

  3. 3

    How to remove double quotes from default string in javascript/Jquery

  4. 4

    Removing the double quotes from a string

  5. 5

    Avoiding escaping double quotes in a string

  6. 6

    Escaping double quotes around argument of onClick in single quote string

  7. 7

    Escaping double quotes in CSS selector

  8. 8

    Escaping double quotes in a variable in a shell script

  9. 9

    double quotes escaping in golang exec

  10. 10

    External table in HIVE - Escaping double quotes from original data set

  11. 11

    Perl: Escaping the double-quotes expanded from a variable

  12. 12

    Java - escaping double quotes in string from file

  13. 13

    Passing a variable with escaping double quotes over ssh

  14. 14

    XSLT 1.0 escaping double quotes and backslash in a string

  15. 15

    Pass a string with double and single quotes from asp to javascript function

  16. 16

    Escaping a string with quotes in Laravel

  17. 17

    replace single quotes and double quotes from a string

  18. 18

    Passing an array to a function and escaping double quotes in Javascript

  19. 19

    Escaping double quotes around argument of onClick in single quote string

  20. 20

    Escaping double quotes in a variable in a shell script

  21. 21

    External table in HIVE - Escaping double quotes from original data set

  22. 22

    Escaping in double quotes in bash scripts

  23. 23

    Escaping quotes in PHP with variable

  24. 24

    Prevent LOAD DATA INFILE from escaping double double quotes

  25. 25

    Escaping double quotes fix

  26. 26

    Escaping double quotes in perl in Linux

  27. 27

    Escaping double quotes in .csv

  28. 28

    Javascript - double quotes in variable

  29. 29

    How to match a string in double quotes with escaping?

HotTag

Archive