Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34160

Replace a character which is in braces and not enclosed in quotes

I'm trying to replace = with : where it is in a braces and is not inside a single or double quote (converting c# constructor to javascript}:

{name = 'John', something = "a=1", another = 'b=1'}

the result would be:

{name : 'John', something = "a=1", another = 'b=1'}

I have tried a lot and this is the final thing that I came up with but does exactly opposite of what I want (changes only those which are in quotes)

 ss = ss.replace(/({[^}]+([^'"]))(=)((?:[^'"])(?:[^}]+)})/g, '$1:$4');

and this is the result:

{name = 'John', something = "a=1", another = 'b:1'}

and this

/({[^}]+([^'"]))(=)((?:\2)(?:[^}]+)})/g

does not match at all

Upvotes: 0

Views: 67

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174736

You may try this,

var s = "{name = 'John', something = \"a=1\", another = 'b=1'}";

console.log(s.replace(/=(?=\s*(['"])((?!\1).)*\1)/g, ':'))

Upvotes: 1

Related Questions