Improved regex matching, docs updates
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
diff --git a/components/prism-javascript.js b/components/prism-javascript.js
index 5921658..79ed187 100644
--- a/components/prism-javascript.js
+++ b/components/prism-javascript.js
@@ -6,7 +6,7 @@ Prism.languages.javascript = {
},
'string': /("|')(\\?.)*?\1/g,
'regex': {
- pattern: /(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\r\n])+\/[gim]{0,3}/g,
+ pattern: /(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\r\n])+\/[gim]{0,3}(?=\s*($|[\r\n,.;})]))/g,
lookbehind: true
},
'keyword': /\b(var|let|if|else|while|do|for|return|in|instanceof|function|new|with|typeof|try|catch|finally|null|break|continue)\b/g,
diff --git a/components/prism-javascript.min.js b/components/prism-javascript.min.js
index e77ec3c..1d1f569 100644
--- a/components/prism-javascript.min.js
+++ b/components/prism-javascript.min.js
@@ -1 +1 @@
-Prism.languages.javascript={comment:/\/\*[\w\W]*?\*\//g,"line-comment":{pattern:/(^|[^\\])\/\/.*?(\r?\n|$)/g,lookbehind:!0},string:/("|')(\\?.)*?\1/g,regex:{pattern:/(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\r\n])+\/[gim]{0,3}/g,lookbehind:!0},keyword:/\b(var|let|if|else|while|do|for|return|in|instanceof|function|new|with|typeof|try|catch|finally|null|break|continue)\b/g,"boolean":/\b(true|false)\b/g,number:/\b-?(0x)?\d*\.?\d+\b/g,operator:/[-+]{1,2}|!|=?<|=?>|={1,2}|(&){1,2}|\|?\||\?|\*|\//g,ignore:/&(lt|gt|amp);/gi,punctuation:/[{}[\];(),.:]/g};Prism.languages.markup&&Prism.languages.insertBefore("markup","tag",{script:{pattern:/(<|<)script[\w\W]*?(>|>)[\w\W]*?(<|<)\/script(>|>)/ig,inside:{tag:{pattern:/(<|<)script[\w\W]*?(>|>)|(<|<)\/script(>|>)/ig,inside:Prism.languages.markup.tag.inside},rest:Prism.languages.javascript}}});
\ No newline at end of file
+Prism.languages.javascript={comment:/\/\*[\w\W]*?\*\//g,"line-comment":{pattern:/(^|[^\\])\/\/.*?(\r?\n|$)/g,lookbehind:!0},string:/("|')(\\?.)*?\1/g,regex:{pattern:/(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\r\n])+\/[gim]{0,3}(?=\s*($|[\r\n,.;})]))/g,lookbehind:!0},keyword:/\b(var|let|if|else|while|do|for|return|in|instanceof|function|new|with|typeof|try|catch|finally|null|break|continue)\b/g,"boolean":/\b(true|false)\b/g,number:/\b-?(0x)?\d*\.?\d+\b/g,operator:/[-+]{1,2}|!|=?<|=?>|={1,2}|(&){1,2}|\|?\||\?|\*|\//g,ignore:/&(lt|gt|amp);/gi,punctuation:/[{}[\];(),.:]/g};Prism.languages.markup&&Prism.languages.insertBefore("markup","tag",{script:{pattern:/(<|<)script[\w\W]*?(>|>)[\w\W]*?(<|<)\/script(>|>)/ig,inside:{tag:{pattern:/(<|<)script[\w\W]*?(>|>)|(<|<)\/script(>|>)/ig,inside:Prism.languages.markup.tag.inside},rest:Prism.languages.javascript}}});
\ No newline at end of file
diff --git a/examples.html b/examples.html
index e02de9b..9e8d8e8 100644
--- a/examples.html
+++ b/examples.html
@@ -188,6 +188,12 @@ var comment = /\/\*[\w\W]*?\*\//g;</code></pre>
<h2>Regex inside single line comment</h2>
<pre><code>// hey, /this doesn’t fail!/ :D</code></pre>
+
+ <h2>Two or more division operators on the same line</h2>
+ <pre><code>var foo = 5 / 6 / 7;</code></pre>
+
+ <h2>A division operator on the same line as a regex, preceding it</h2>
+ <pre><code>var foo = 1/2, bar = /a/g;</code></pre>
</section>
<section id="failures" class="language-javascript">
@@ -201,11 +207,8 @@ var comment = /\/\*[\w\W]*?\*\//g;</code></pre>
<h2>Comment-like substrings</h2>
<pre><code>"foo /* bar */ baz"; "foo // bar";</code></pre>
- <h2>Strings inside regexes</h2>
+ <h2>Two quotes of the same type (i.e. single or double) inside a regex</h2>
<pre><code>/"foo"/;</code></pre>
-
- <h2>Two or more division operators on the same line</h2>
- <pre><code>5 / 6 / 7</code></pre>
</section>
<footer data-src="templates/footer.html" data-type="text/html"></footer>
diff --git a/faq.html b/faq.html
index a1a58c0..efedb85 100644
--- a/faq.html
+++ b/faq.html
@@ -26,6 +26,13 @@
</header>
<section>
+ <h1>This page doesn’t work in Opera!</h1>
+
+ <p><strong>Prism works fine in Opera.</strong> However, this page might sometimes appear to not be working in Opera, due to the theme switcher triggering an Opera bug.
+ This will be fixed soon.</p>
+</section>
+
+<section>
<h1>Isn’t it bad to do syntax highlighting with regular expressions?</h1>
<p>It is true that to correctly handle every possible case of syntax found in the wild, one would need to write a full-blown parser.
diff --git a/index.html b/index.html
index cfa46b1..1578e0f 100644
--- a/index.html
+++ b/index.html
@@ -63,7 +63,7 @@
<p>This page’s logo (SVG), highlighted with Prism:</p>
<pre data-src="logo.svg"></pre>
- <p>If you’re still not sold, you can <a href="examples.html">view more examples</a>.</p>
+ <p>If you’re still not sold, you can <a href="examples.html">view more examples</a> or <a href="test.html">try it out for yourself</a>.</p>
</section>
<section id="features-full">
@@ -74,7 +74,7 @@
<li>Very easy to extend without modifying the code, due to Prism’s <a href="#plugins">plugin architecture</a>. Multiple hooks are scattered throughout the source.</li>
<li>Very easy to <a href="extending.html#language-definitions">define new languages</a>. Only thing you need is a good understanding of regular expressions</li>
<li>All styling is done through CSS, with <a href="faq.html#how-do-i-know-which-tokens-i-can-style-for">sensible class names</a> rather than ugly namespaced abbreviated nonsense.</li>
- <li>Wide browser support: IE9+, Firefox, Chrome, Safari, Opera, most Mobile browsers</li>
+ <li>Wide browser support: IE9+, Firefox, Chrome, Safari, <a href="faq.html#this-page-doesnt-work-in-opera">Opera</a>, most Mobile browsers</li>
<li>Highlights embedded languages (e.g. CSS inside HTML, JavaScript inside HTML)</li>
<li>Highlights inline code, not just code blocks</li>
<li>Highlight specific lines and/or line ranges (requires <a href="plugins/line-highlight/">plugin</a>)</li>
@@ -86,7 +86,7 @@
<h1>Limitations</h1>
<ul>
<li>Any pre-existing HTML in the code will be stripped off. <a href="faq.html#if-pre-existing-html-is-stripped-off-how-can-i-highlight">There are ways around it though</a>.</li>
- <li>Regex-based so it *will* fail on certain edge cases.</li>
+ <li>Regex-based so it *will* fail on <a href="examples.html#failures">certain edge cases</a>.</li>
<li>No IE 6-8 support. If someone can read code, they are probably in the 85% of the population with a modern browser.</li>
</ul>
</section>
diff --git a/prism.js b/prism.js
index 68f211e..74bc30f 100644
--- a/prism.js
+++ b/prism.js
@@ -393,7 +393,7 @@ Prism.languages.javascript = {
},
'string': /("|')(\\?.)*?\1/g,
'regex': {
- pattern: /(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\r\n])+\/[gim]{0,3}/g,
+ pattern: /(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\r\n])+\/[gim]{0,3}(?=\s*($|[\r\n,.;})]))/g,
lookbehind: true
},
'keyword': /\b(var|let|if|else|while|do|for|return|in|instanceof|function|new|with|typeof|try|catch|finally|null|break|continue)\b/g,