diff --git a/CHANGES.md b/CHANGES.md
index c12ea5f3ed..e64932fdc4 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -10,6 +10,7 @@ New themes:
Core Changes:
+- improve regular expression detect (less false-positives) (#2380) [Josh Goebel][]
- make `noHighlightRe` and `languagePrefixRe` configurable (#2374) [Josh Goebel][]
Language Improvements:
diff --git a/src/highlight.js b/src/highlight.js
index 201a8287f9..bd15c8393b 100644
--- a/src/highlight.js
+++ b/src/highlight.js
@@ -1102,17 +1102,26 @@ https://highlightjs.org/
relevance: 0
};
hljs.REGEXP_MODE = {
- className: 'regexp',
- begin: /\//, end: /\/[gimuy]*/,
- illegal: /\n/,
- contains: [
- hljs.BACKSLASH_ESCAPE,
- {
- begin: /\[/, end: /\]/,
- relevance: 0,
- contains: [hljs.BACKSLASH_ESCAPE]
- }
- ]
+ // this outer rule makes sure we actually have a WHOLE regex and not simply
+ // an expression such as:
+ //
+ // 3 / something
+ //
+ // (which will then blow up when regex's `illegal` sees the newline)
+ begin: /(?=\/[^\/\n]*\/)/,
+ contains: [{
+ className: 'regexp',
+ begin: /\//, end: /\/[gimuy]*/,
+ illegal: /\n/,
+ contains: [
+ hljs.BACKSLASH_ESCAPE,
+ {
+ begin: /\[/, end: /\]/,
+ relevance: 0,
+ contains: [hljs.BACKSLASH_ESCAPE]
+ }
+ ]
+ }]
};
hljs.TITLE_MODE = {
className: 'title',
diff --git a/test/markup/javascript/regex.expect.txt b/test/markup/javascript/regex.expect.txt
new file mode 100644
index 0000000000..21342a7889
--- /dev/null
+++ b/test/markup/javascript/regex.expect.txt
@@ -0,0 +1,5 @@
+
+`Bad ${foo / 1000}`
+foo = 2 / 2 + 2 / 2;
+foo = / 2 + 2 /;
+b = a++ / 2;
diff --git a/test/markup/javascript/regex.txt b/test/markup/javascript/regex.txt
new file mode 100644
index 0000000000..da0ee68a12
--- /dev/null
+++ b/test/markup/javascript/regex.txt
@@ -0,0 +1,5 @@
+// examples that have proven problematic in the past
+`Bad ${foo / 1000}`
+foo = 2 / 2 + 2 / 2;
+foo = / 2 + 2 /;
+b = a++ / 2;