How to remove duplicate of any string starting and ending with quotes in javascript
I have below line in javascript:
var str = '"restoreState": "restoreState":0,';
duplicate substring "restoreState":
. I want to use regex to remove duplicate substring. I tried the code below but didn't work:
var re = str.replace(/([a-zA-Z]).*?\1/, '')
console.log(re)
conclusion "\"eState\": \"restoreState\":0,"
. How do I remove this with regular expressions? what is wrong with my regex?
I am looking for a common strings solution. Remove the duplicate part of the string, if it has a different option, do nothing in the string. For examepl:
"restoreState": "restoreState":0,
=> "restoreState":0,
"restoreState": 0
=> "restoreState": 0
source to share
Based on comments:
function dedupe(str) {
return str.replace(/("[^"]+")(?=.*\1)/g, '')
}
console.log(dedupe('"foo": "bar", "bar": "foo"'));
console.log(dedupe('"foo": { "foo": "bar" }'));
console.log(dedupe('"foo": "bar": "foo": "bar": 0'));
console.log(dedupe('"foo": "baz", "bar": "baz"'));
console.log(dedupe('"foo": 17, "bar": 17'));
Capture a string: quote, any number of non-quotes, quote. Make sure there is one more. If so, replace it with nothing.
source to share