I have such strings which are received from a .pck
file:
'{"aaa":"sssbbb"}'
'{"aaa":"ssscde"}'
I need to delete everything after the backslash (including the backslash) till the first "
, respectively till the end of current value. So, I expect this result for both of those strings:
'{"aaa":"sss"}'
'{"aaa":"sss"}'
I can’t really parse this and can’t really string replace the backslash with anything else. I have tried to get the index of the backslash using str.indexOf("\");
, but got -1
. I have tried lots of regex expressions, but did not work. I have tried to replace str.replace(String.charCodeAt(92))
, but did not work either.
2
Answers
You can use a regular expression to match the portion of the string you want to remove.
Example:
#original string
let str = ‘{"aaa":"sssbbb"}’;
// Regular expression to match the backslash and everything after it
let updatedStr = str.replace(/[^",]*/g, ”);
// Output the updated string
console.log(updatedStr);
You can pass a reviver function to correct the values upon parsing.