Skip to content

Commit fab013b

Browse files
committed
More readable regex
1 parent 951dba4 commit fab013b

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

webroot/js/component/output.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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+)?(?:[eE][+-]?\d+)?)(?=\s*[,\]\}\n]|$)/g, '$1<span class="json-number">$2</span>')
22-
.replace(/(:\s*|[\[\,]\s*)(true|false)(?=\s*[,\]\}\n]|$)/g, '$1<span class="json-boolean">$2</span>')
23-
.replace(/(:\s*|[\[\,]\s*)(null)(?=\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

2659
function formatOutput(data) {

0 commit comments

Comments
 (0)