-1

I am trying to replace the string {NUM} in the following variable:

var table = '        <table class="full-width">\r\n' +
            '            <tbody>\r\n' +
            '            <tr>\r\n' +
            '                <td width="75%" class="border-right-dotted left-line-tab">\r\n' +
            '                    <span id="47_TOTAL-CHARGES_D_{NUM}" class="input-text"></span>\r\n' +
            '                </td>\r\n' +
            '                <td width="25%" class="center-text">\r\n' +
            '                    <span id="47_TOTAL-CHARGES_C_{NUM}" class="input-text"></span>\r\n' +
            '                </td>\r\n' +
            '            </tr>\r\n' +
            '            </tbody>\r\n' +
            '        </table>\r\n';

using the following jquery replace: table = table.replace("/{NUM}/gm", num);

but it doesn't seem to work. Testing the regex in https://regex101.com/ seems to present that the regex is fine, but it still does not replace the text as expected.

enter image description here

Bitz
  • 1,128
  • 11
  • 33

1 Answers1

4

Just remove the double quotes:

table = table.replace(/{NUM}/gm, num);

A text within double quotes defines a string literal whilst you need a regular expression literal, which should be surrounded by slashes.

Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40