Alright, so I'm currently working on a simplistic manner of mimicking the function of PHP's urlencode() with JS's escape() and some string replacement. I'm purely looking at keyboard characters, so no special ASCII or Unicode characters. The problem I'm encountering is that the specific characters *, +, and / all have special meanings in RegExp, and seeing as JavaScript String Object methods use RegExp parameters, I cannot fully replicate PHP's urlencode(). Thus, how would one manage to perform a replacement of these characters within a string using JS?
Background information:
escape() discrepancies with urlencode():
@
: not converted, should be %40
&
: considered an html entity, thus is escaped as %26amp%3B rather than %26
*
: not converted, should be %2A
+
: not converted, should be %2B
/
: not converted, should be %2F
<
: considered an html entity, thus is escaped as %26lt%3B rather than %3C
>
: considered an html entity, thus is escaped as %26gt%3B rather than %3E
HTML Entity conversion is as simple as
str.replace(/&/g, '%26').replace(/</g, '%3C').replace(/>/g, '%3E');
and @
can be replaced, as it is not a reserved RegExp character.
*
represents the {0}
conditional
+
represents the {1}
conditional
/
is the basic RegExp tag character
Thank you for your time.