Improve Regex highlighting (#28183)

chbk created

| Zed 0.180.2 | With this PR |
| --- | --- |
|
![Image](https://github.com/user-attachments/assets/e840bd81-25ff-4c7a-af03-bac6db11f910)
|
![Image](https://github.com/user-attachments/assets/3fd58164-8992-44e1-be01-8c6d70f9587d)
|

```js
match = "424242"
regex = /(42)+?\d{2}\1/g
```

- `/`: `operator` -> `string.regex` (like `"` for regex strings)
- `+?`: `operator.regex`
- `\d`: `string.escape` -> `string.escape.regex`
- `\1`: `keyword.operator.regex` (backreference)
- `/g`: `keyword.regex` -> `keyword.operator.regex`
- `{2}`: `number` -> `number.quantifier.regex`

Release Notes:

  - Improved Regex highlighting

Change summary

crates/languages/src/javascript/highlights.scm | 4 +++-
crates/languages/src/regex/highlights.scm      | 6 ++++--
crates/languages/src/tsx/highlights.scm        | 4 +++-
crates/languages/src/typescript/highlights.scm | 4 +++-
4 files changed, 13 insertions(+), 5 deletions(-)

Detailed changes

crates/languages/src/javascript/highlights.scm 🔗

@@ -85,7 +85,7 @@
 (escape_sequence) @string.escape
 
 (regex) @string.regex
-(regex_flags) @keyword.regex
+(regex_flags) @keyword.operator.regex
 (number) @number
 
 ; Tokens
@@ -145,6 +145,8 @@
   "??="
 ] @operator
 
+(regex "/" @string.regex)
+
 [
   "("
   ")"

crates/languages/src/regex/highlights.scm 🔗

@@ -20,7 +20,7 @@
   (control_letter_escape)
   (character_class_escape)
   (control_escape)
-] @string.escape
+] @string.escape.regex
 
 [
   "*"
@@ -32,17 +32,19 @@
   (start_assertion)
   (end_assertion)
   (any_character)
+  (lazy)
 ] @operator.regex
 
 [
   (boundary_assertion)
   (non_boundary_assertion)
   (backreference_escape)
+  (decimal_escape)
 ] @keyword.operator.regex
 
 (count_quantifier
   [
-    (decimal_digits) @number
+    (decimal_digits) @number.quantifier.regex
     "," @punctuation.delimiter.regex
   ])
 

crates/languages/src/tsx/highlights.scm 🔗

@@ -86,7 +86,7 @@
 (escape_sequence) @string.escape
 
 (regex) @string.regex
-(regex_flags) @keyword.regex
+(regex_flags) @keyword.operator.regex
 (number) @number
 
 ; Tokens
@@ -147,6 +147,8 @@
   "??="
 ] @operator
 
+(regex "/" @string.regex)
+
 [
   "("
   ")"

crates/languages/src/typescript/highlights.scm 🔗

@@ -113,7 +113,7 @@
 (escape_sequence) @string.escape
 
 (regex) @string.regex
-(regex_flags) @keyword.regex
+(regex_flags) @keyword.operator.regex
 (number) @number
 
 ; Tokens
@@ -166,6 +166,8 @@
   "??="
 ] @operator
 
+(regex "/" @string.regex)
+
 (ternary_expression
   [
     "?"