You need to find ,
, after which there is no any new attribute, object or array.
New attribute could start either with quotes ("
or '
) or with any word-character (w
).
New object could start only with character {
.
New array could start only with character [
.
New attribute, object or array could be placed after a bunch of space-like symbols (s
).
So, the regex will be like this:
let regex = /,(?!s*?[{["'w])/g;
Use it like this:
// javascript
let input; // this is the initial string of data
let correct = input.replace(regex, ''); // remove all trailing commas
let data = JSON.parse(correct); // build a new JSON object based on correct string
Try the first regex.
Another approach is to find every ,
, after which there is a closing bracket.
Closing brackets in this case are }
and ]
.
Again, closing brackets might be placed after a bunch of space-like symbols (s
).
Hence the regexp:
let regex = /,(?=s*?[}]])/g;
Usage is the same.
Try the second regex.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…