-2

I have a textearea called note_text, a div called note_preview and this function:

function fence_math(text) {
    return text
        .replace(new RegExp('\\\((.*?)\\\)', 'g'), '`memviv-math $1`')
        .replace(new RegExp('\\\[(.*?)\\\]', 'g'), '`memviv-equation $1`');
}

If I run this:

note_preview.innerHTML = fence_math(note_text.value);

When note_text contains

\(a *b* c\)

I get

<div id="note_preview">\`memviv-math a *b* c\`</div>

Why are there backslashes before the accents ? I would have expected:

<div id="note_preview">`memviv-math a *b* c`</div>

What can I do to get the proper result ?

adiga
  • 34,372
  • 9
  • 61
  • 83
vkubicki
  • 1,104
  • 1
  • 11
  • 26

1 Answers1

0

When you get value from the input it escaping special characters / in your case that's why your note_text.value is "\\(a *b* c\\)". Try to add extra backslashes to escape to your RegExp functions

.replace(new RegExp('\\\\((.*?)\\\\)', 'g'), '`memviv-math $1`')
.replace(new RegExp('\\\\[(.*?)\\\\]', 'g'), '`memviv-equation $1`');
svyat1s
  • 868
  • 9
  • 12
  • 21
  • 1
    Thank you, I forgot that Javascript escaped characters in strings. '\\\\\\((.*?)\\\\\\)' solved my problem. – vkubicki Oct 07 '20 at 19:22
  • No problem. You could mark that answer as accepted if it helped) – svyat1s Oct 07 '20 at 19:26
  • No need for another answer, it is already [172nd post](https://stackoverflow.com/questions/linked/17863066?lq=1) on the same topic. Please consider removing it. – Wiktor Stribiżew Oct 07 '20 at 20:09