-1

When I do it like this:

areport = areport.replace(/\\"/g, "");
areport = areport.replace(/{/g, "");
areport = areport.replace(/}/g, "");
areport = areport.replace(/[\[\]']+/g, "");
areport = areport.replace(/,/g, "");
areport = areport.replace(/"/g, "");
areport = areport.replace(/\\/g, "");
areport = areport.replace(/null/g, "");

It works, however when I do it like this:

areport = areport.replace(/\[\]\/\\,\{\}\"null/g, "");

It doesn't. I've checked it with 'regex101' and it returns "g modifier: global. All matches (don't return after first match)" so I attempted to rearrange the order but to no avail. Please show me the error of my ways. Thanks.

fyrfterjr
  • 17
  • 3

2 Answers2

1

Your code is looking for the following exact string: []/\,{}"null. If you want to look for any instance of any of those characters, you need to put them in square brackets, which function to search for "any of these characters." The null can then be put after an or | character.

/[[\]/\\,{}"]|null/g

TFrazee
  • 687
  • 5
  • 18
0

Thank you all for your help. The answer was to add '|' in between each character

areport = areport.replace(/\\|\{|\}|\[|\]|\\|,|\"|null/g, "");

so that regex knew to look for any global instance instead of the particular order. It's interesting that in I couldn't find any reference to that in any of the resources that I checked (must not have looked hard enough). Additional thanks to TFrazee for pointing that out.

fyrfterjr
  • 17
  • 3