0

I was converting normal string in to latex format.So i was created the latex code match and replace the \ single slash into \\ double slash.why the i need it Refer this link.I tried Below code :

function test(){
  var tex="$$\left[ x=\left({{11}\over{2}}+{{\sqrt{3271}}\over{2\,3^{{{3}\over{2} $$";
   var tex_form = tex.replace("/[\\\/\\\\\.\\\\]/g", "\\");
  document.getElementById('demo').innerHTML=tex_form;//nothing get
  }
test();
<p id="demo"></p>

Not getting any output data.But the match in this link

i wish to need replace the \ into \\

Community
  • 1
  • 1

3 Answers3

1

There are these issues:

  1. The string literal has no backslashes;

  2. The regular expression is not a regular expression;

  3. The class in the intended regular expression cannot match sequences, only single characters;

  4. The replacement would not add backslashes, only replace with them.

Here you find the details on each point:

1. How to Encode Backslashes in String Literals

Your tex variable has no backslashes. This is because a backslash in a string literal is not taken as a literal backslash, but as an escape for interpreting the character that follows it.

When you have "$$\left...", then the \l means "literal l", and so the content of your variable will be:

$$left...

As an l does not need to be escaped, the backslash is completely unnecessary, and these two assignments result in the same string value:

var tex="$$\left[ x=\left({{11}\over{2}}+{{\sqrt{3271}}\over{2\,3^{{{3}\over{2} $$";

var tex="$$left[ x=left({{11}over{2}}+{{sqrt{3271}}over{2,3^{{{3}over{2} $$";

To bring the point home, this will also represent the same value:

var tex="\$\$\l\e\f\t\[\ \x\=\l\e\f\t\(\{\{\1\1\}\o\v\e\r\{\2\}\}\+\{\{\s\q\r\t\{\3\2\7\1\}\}\o\v\e\r\{\2\,\3\^\{\{\{\3\}\o\v\e\r\{\2\}\ \$\$";

If you really want to have literal backslashes in your content (which I understand you do, as this is about LaTeX), then you need to escape each of those backslashes... with a backslash:

var tex="$$\\left[ x=\\left({{11}\\over{2}}+{{\\sqrt{3271}}\\over{2\\,3^{{{3}\\over{2} $$";

Now the content of your tex variable will be this string:

$$\left[ x=\left({{11}\over{2}}+{{\sqrt{3271}}\over{2\,3^{{{3}\over{2} $$

2. How to Code Regular Expression Literals

You are passing a string literal to the first argument of replace, while you really intend to pass a regular expression literal. You should leave out the quotes for that to happen. The / are the delimiters of a regular expression literal, not quotes:

/[\\\/\\\\\.\\\\]/g

This should not be wrapped in quotes. JavaScript understands the / delimiters as denoting a regular expression literal, including the optional modifiers at the end (like g here).

3. Classes are sets of single characters

This regular expression has unnecessary characters. The class [...] should list all individual characters you want to match. Currently you have these characters (after resolving the escapes):

\
/
\
\
.
\
\

It is overkill to have the backslash represented 5 times. Also, in JavaScript the forward slash and dot do not need to be escaped when occurring in a class. So the above regular expression is equivalent to this one:

/[\\/.]/g

Maybe this is, or is not, what you intended to match. To match several sequences of characters, you could use the | operator. This is just an example:

/\\\\|\\\/|\\\./g

... but I don't think you need this.

4. How to actually prefix with backslashes

It seems strange to me that you would want to replace a point or forward slash with a backslash. Probably you want to prefix those with a backslash. In that case make a capture group (with parentheses) and refer to it with $1 in this replace:

tex.replace(/([\\/.])/g, "\\$1");

Note again, that in the replacement string there is only one literal backslash, as the first one is an escape (see point 1 above).

trincot
  • 317,000
  • 35
  • 244
  • 286
0

why the i need it

As the question you link to says, the \ character has special meaning inside a JavaScript string literal. It represents an escape sequence.

Not getting any output data.But the match in this link

The escape sequence is processed when the string literal is parsed by the JavaScript compiler.

By the time you apply your regular expression to them, they have been consumed. The slash characters only exist in your source code, not in your data.

If you want to put a slash character in your string, then you need to write the escape sequence for it (the \\) in the source code. You can't add them back in with JavaScript afterwards.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

Not sure if I understood the problem, but try this code:

var tex_form = tex.replace("/(\\)/g","\\\\");.  

You need to use '(' ')' instead of '['']' to get a match for output.

dFroze
  • 41
  • 8