Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
PCRE (PHP <7.3)

/
([\s\[\{,:]+)(?!\-?\.?\d|false\W|true\W)([^'\":,\s\]\}]+)([:\s,\}\]_\-\{\[]|$)
/
g

Description

Add single quotes to unquoted parts of a valid JS hash so it can be parsed with JSON decoders. Tries to keep boolean and numeric values unquoted

Note: It will not handle certain edge-cases You might need to clear the trailing commas

Submitted by anonymous - 7 years ago