JQuery - backslash character

I had a problem replacing a backslash character from a string:

var g = myReadString;
g = g.replace("\", "\\\\");

it throws an unrecognized character error.

How could one replace a simple \four \\\\?

I would be grateful for any help, Thank you. Pandas

+3
source share
4 answers

\‍is the beginning of the escape sequence. If you want to write \‍literally, you need to write \\, which is also an escape sequence, and will be interpreted as one \‍. Therefore, if you want to replace one \‍with four \\\\, you need to write this:

g.replace("\\", "\\\\\\\\")

\‍. , :

g.replace(/\\/g, "\\\\\\\\")
+7
g = g.replace(/\\/g, "\\\\");

, . , .

0

. ... http://www.c-point.com/javascript_tutorial/special_characters.htm

, , . , , . ? , - .

var g = myReadString;
g = g.replace("\\", "\\\\");

, !

0

, , .

In your first argument for, replace()you intend to pass a string containing \, but it ends as ", (quote-comma-space)! This is because you really avoid “closing” the quotation in a line:

g = g.replace("\", "\\\\");
              ^    ^
              s    e
              t    n
              a    d
              r
              t

Now the first argument is the quote-comma-space string. The rest gives a syntax error!

What did you want:

g = g.replace("\\", "\\\\\\\\");
              ^  ^  ^        ^
              s  e  s        e
              t  n  t        n
              a  d  a        d
              r     r
              t     t

First argument: string \
Second argument: string\\\\

0
source

Source: https://habr.com/ru/post/1777913/


All Articles