I have a regex
(\w+)\s*:((?:\w+[-+*%])*?\w+)$
that matches key value pair like these -
key:value
key2:value2
But the regex match fails if the key and value are within quotes like these -
"key":value
"key2":"value2"
What modification can be done to make the regex match key and value within quotes also ?
You can use optional quotes on either side of key-value
pairs like this:
/("?)\b(\w+)\1\s*:\s*("?)((?:\w+[-+*%])*?\w+)\b\3/g
Take note of group ("?)
that captured an empty string or a double quote. On the other side we use a back-reference \\1
of this group for closing quote.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.