-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.

6
  • I think you seek /null|[\\",[\]'{}]+/g Commented Jun 19, 2018 at 16:37
  • Thank you but still didn't work. Commented Jun 19, 2018 at 16:44
  • Can you provide us with an example of the input and the desired output? Commented Jun 19, 2018 at 16:45
  • The input: let areport = JSON.stringify(localStorage.getItem("report")); Commented Jun 19, 2018 at 16:51
  • I just need all the added JSON characters removed. Commented Jun 19, 2018 at 16:52

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

It is exactly my /null|[\\",[\]'{}]+/g pattern.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.