@@ -15,12 +15,45 @@ function highlightJSON(json) {
1515 json = JSON . stringify ( json , null , 2 ) ;
1616 }
1717
18+ // Common regex patterns for JSON syntax highlighting
19+
20+ // Matches optional whitespace (spaces, tabs, newlines)
21+ // Examples: "", " ", "\t", "\n", " \n "
22+ const optionalWhitespace = '\\s*' ;
23+
24+ // Matches JSON characters that precede a value: colon followed by optional whitespace, or array/object start with optional whitespace
25+ // Examples: ": ", ":[", ", ", "[ ", "[42"
26+ const valuePrefixes = '(:\\s*|[\\[\\,]\\s*)' ;
27+
28+ // Matches JSON characters that follow a value: comma, closing brackets/braces, newline, or end of string
29+ // Examples: ",", "]", " }", "\n", end of string
30+ const valueEndings = '(?=\\s*[,\\]\\}\\n]|$)' ;
31+
32+ // Matches quoted strings with escaped characters
33+ // Examples: "hello", "world \"quoted\"", "path\\to\\file", "unicode: \\u0041"
34+ const quotedString = '"(?:[^"\\\\]|\\\\.)*"' ;
35+
36+ // Matches JSON object keys (quoted strings containing word chars, spaces, underscores, hyphens)
37+ // Examples: "name", "user_id", "first-name", "my key", "data_2023"
38+ const objectKey = '"[\\w\\s_-]+"' ;
39+
40+ // Matches numbers (integer, decimal, scientific notation)
41+ // Examples: 42, -17, 3.14, -0.5, 1e10, 2.5e-3, 1E+5
42+ const number = '-?\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?' ;
43+
44+ // Complete regex patterns for each replacement
45+ const keyPattern = new RegExp ( `(${ objectKey } )(${ optionalWhitespace } :)` , 'g' ) ; // "name": or "user_id" :
46+ const stringValuePattern = new RegExp ( `(:\\s*)(${ quotedString } )` , 'g' ) ; // : "John Doe" or :"hello"
47+ const numberPattern = new RegExp ( `${ valuePrefixes } (${ number } )${ valueEndings } ` , 'g' ) ; // : 42, [123, or , -3.14
48+ const booleanPattern = new RegExp ( `${ valuePrefixes } (true|false)${ valueEndings } ` , 'g' ) ; // : true, [false, or , true
49+ const nullPattern = new RegExp ( `${ valuePrefixes } (null)${ valueEndings } ` , 'g' ) ; // : null, [null, or , null
50+
1851 return json
19- . replace ( / ( " [ \w \s _ - ] + " ) ( \s * : ) / g , '<span class="json-key">$1</span>$2' )
20- . replace ( / ( : \s * ) ( " (?: [ ^ " \\ ] | \\ . ) * " ) / g , '$1<span class="json-string">$2</span>' )
21- . replace ( / ( : \s * | [ \[ \, ] \s * ) ( - ? \d + (?: \. \d + ) ? (?: [ e E ] [ + - ] ? \d + ) ? ) (? = \s * [ , \] \} \n ] | $ ) / g , '$1<span class="json-number">$2</span>' )
22- . replace ( / ( : \s * | [ \[ \, ] \s * ) ( t r u e | f a l s e ) (? = \s * [ , \] \} \n ] | $ ) / g , '$1<span class="json-boolean">$2</span>' )
23- . replace ( / ( : \s * | [ \[ \, ] \s * ) ( n u l l ) (? = \s * [ , \] \} \n ] | $ ) / g , '$1<span class="json-null">$2</span>' ) ;
52+ . replace ( keyPattern , '<span class="json-key">$1</span>$2' )
53+ . replace ( stringValuePattern , '$1<span class="json-string">$2</span>' )
54+ . replace ( numberPattern , '$1<span class="json-number">$2</span>' )
55+ . replace ( booleanPattern , '$1<span class="json-boolean">$2</span>' )
56+ . replace ( nullPattern , '$1<span class="json-null">$2</span>' ) ;
2457}
2558
2659function formatOutput ( data ) {
0 commit comments