javascriptsplitmalformed

JavaScript Find Where Splited Character İn String


str="#%%90safas'+!%27%";

i open "for" and try split all "%" character in str variable.

If str.split("%")[i].substr(0,2).length !=2 then replace it to %25 in str variable

so i want to make: str="#%25%90safas'+!%27%25";

I am trying to solve URI Malformed error with JavaScript for decodeURIComponent. But i don't know how to choose which splited percent characters substr(0,2).length isn't 2 then replace it to "%25"

Can anyone help me?


Solution

  • You appear to be suffering from the X-Y problem. Checking if the substring is length 2 won't actually do anything useful. Instead, you can use a regex with a negative lookahead:

    str = str.replace(/%(?![0-9a-f]{2})/ig, "%25");