SyntaxError: nothing to repeat. ReferenceError ... not defined

I have a line in a JavaScript program that replaces all plus-signs in a string with %2B, which is the way it would be encoded in a URL. This is the line:

  mystring.replace(/+/g, "%2B");
and it gave me a weird error.
SyntaxError: nothing to repeat demo3D.js:1
ReferenceError: demo3D is not defined myProgram.html:57:3

This fixed it:

  mystring.replace(/\+/g, "%2B");
The plus sign needed to be escaped. This is odd because I expected it to be OK without escaping, as in the shell command:

  echo "one+two" | sed "s/+/,/g" 



-MCW April 25, 2021