Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(Assuming that you are looking for a JavaScript regexp literal, delimited by <code>/</code>.) </p> <p>It would be simple enough to just look for everything in between <code>/</code>, but that might not always be a regexp. For example, such a search would return <code>/2 + 3/</code> of the string <code>var myNumber = 1/2 + 3/4</code>. This means that you will have to know what occurs before the regular expression. The regexp should be preceded by something other than a variable or number. These are the cases that I can think of:</p> <pre><code>/regex/; var myVar = /regex/; myFunction(/regex/,/regex/); return /regex/; typeof /regex/; case /regex/; throw /regex/; void /regex/; "global" in /regex/; </code></pre> <p>In some languages you can use lookbehind, which might look like this (untested!): </p> <pre class="lang-regex prettyprint-override"><code>(?=&lt;^|\n|[^\s\w\/]|\breturn|\btypeof|\bcase|\bthrow|\bvoid|\bin)\s*\/(?:\\\/|[^\/\*\n])(?:\\\/|[^\/\n])*\/ </code></pre> <p>However, JavaScript does not support that. I would recommend imitating lookbehind by putting the portion of the regexp designed to match the literal itself in a capturing group and accessing that. All cases of which I am aware can be matched by this regexp:</p> <pre class="lang-regex prettyprint-override"><code>(?:^|\n|[^\s\w\/]|\breturn|\btypeof|\bcase|\bthrow|\bvoid|\bin)\s*(\/(?:\\\/|[^\/\*\n])(?:\\\/|[^\/\n])*\/) </code></pre> <p><strong>NOTE</strong>: This regex sometimes results in false positives in comments.</p> <p>If you want to also grab modifiers (e.g. <code>/regex/gim</code>), use</p> <pre class="lang-regex prettyprint-override"><code>(?:^|\n|[^\s\w\/]|\breturn|\btypeof|\bcase|\bthrow|\bvoid|\bin)\s*(\/(?:\\\/|[^\/\*\n])(?:\\\/|[^\/\n])*\/\w*) </code></pre> <p>If there are any reserved words I am missing that may be followed by a regexp literal, simply add this to the end of the first group: <code>|\b</code><strong>keyword</strong></p> <p>All that remains then is to access the capturing group, using a code <em>similar</em> to the following:</p> <pre><code>var codeString = "function(){typeof /regex/;}"; var searchValue = /(?:^|\n|[^\s\w\/]|\breturn|\btypeof|\bcase|\bthrow)\s*(\/(?:\\\/|[^\/\*\n])(?:\\\/|[^\/\n])*\/)/g; // the global modifier is necessary! var match = searchValue.exec(codeString); // "['typeof /regex/','/regex/']" match = match[1]; // "/regex/" </code></pre> <p><strong>UPDATE</strong><br /> I just fixed an error with the regexp concerning escaped slashes that would have caused it to get only <code>/\/</code> of a regexp like <code>/\/hello/</code></p> <p><strong>UPDATE</strong> 4/6<br /> Added support for <code>void</code> and <code>in</code>. You can't blame me too much for not including this at first, as even Stack Overflow doesn't, if you look at the syntax coloring in the first code block.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload