0

How to replace "?test=666&test" to "222" in the following code?

If my code has "?" or "&", it will not work.

<body>
  hello
  <br>
  ?test=666&test

  <script>
    function rp (str, map) {
      for (var i in map) {
        str = str.replace(new RegExp(i, 'g'), map[i]);
      }
      return str;
    }

    document.body.innerHTML = rp(document.body.innerHTML, {
        'hello': '111',
        '?test=666&test': '222'
      });
  </script>
</body>
beaver
  • 523
  • 1
  • 9
  • 20
Ken.c
  • 19
  • 1
  • 1
    You escape them (and any other characters that have special meaning in the regex), more: https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript – T.J. Crowder May 16 '18 at 10:54
  • ? is wild char for Regex, You need to escape them if you want to use themselves in a string. Ex: ?test=666&test has to given like this, \?test=666\&test. As @PeterB said not sure about & char – Hassan Mudassir May 16 '18 at 10:55
  • You need to use `\\?` in the string. This way the string will contain `\?`. Only this will work in the `RegExp` constructor. – Sebastian Simon May 16 '18 at 11:06
  • Possible duplicate of [escaping question mark in regex javascript](https://stackoverflow.com/questions/889957/escaping-question-mark-in-regex-javascript) – Sebastian Simon May 16 '18 at 11:07

2 Answers2

0

? and & and several other characters have special meaning in regular expressions. You need to escape them (with a \) to use them literally.

This question's answers describe doing so in the general case.

Using a hypothetical escapeRegex function derived from those answers, you'd do:

str = str.replace(new RegExp(escapeRegex(i), 'g'), map[i]);

To just handle ? and &, you'd do this (but don't do this, it leaves the door open for other special chars):

str = str.replace(new RegExp(i.replace(/[?&]/g, "\\$&")., 'g'), map[i]);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • `str = str.replace(new RegExp(escapeRegex(i), 'g'), map[i]); ` this is not work . could you help me again? – Ken.c May 17 '18 at 02:07
0

You need to take care of escaping and encoding in various different ways:

  1. In regex, ? is a modifier. You need to escape it and use \? instead.
  2. In a javascript string literal, \ is a special character. To get a single backslash in a string you need to write '\\', and to get \? in a string you need to write '\\?'
  3. In HTML, a simple & translates to &amp;. To match that encoded character, you need to look for &amp;

This is the result of what you should use:

function rp(str, map) {
  for (var i in map) {
    str = str.replace(new RegExp(i, 'g'), map[i]);
  }
  return str;
}
document.body.innerHTML = rp(document.body.innerHTML, {
  'hello': '111',
  '\\?test=666&amp;test': '222'
});
<body>
  hello
  <br>
  ?test=666&test
</body>
Peter B
  • 22,460
  • 5
  • 32
  • 69
  • thanks. it's cool. and i want another way. i want to change my code , not change the `?test=666&test`. thanks a lot. – Ken.c May 17 '18 at 02:20