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>

4
  • 1
    You escape them (and any other characters that have special meaning in the regex), more: stackoverflow.com/questions/3561493/… Commented May 16, 2018 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 Commented May 16, 2018 at 10:55
  • You need to use \\? in the string. This way the string will contain \?. Only this will work in the RegExp constructor. Commented May 16, 2018 at 11:06
  • Possible duplicate of escaping question mark in regex javascript Commented May 16, 2018 at 11:07

2 Answers 2

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]);
Sign up to request clarification or add additional context in comments.

1 Comment

str = str.replace(new RegExp(escapeRegex(i), 'g'), map[i]); this is not work . could you help me again?
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>

1 Comment

thanks. it's cool. and i want another way. i want to change my code , not change the ?test=666&test. thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.