-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
  • 31,610
  • 8
  • 53
  • 74
vkubicki
  • 1,086
  • 1
  • 9
  • 21

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`');
ukr.svyat
  • 838
  • 9
  • 12
  • 20