Regular Expressions 101

Community Patterns

Quote Javascript-style hash to convert to valid JSONP (JSON with padding)

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 - 6 years ago