Note that there are some explanatory texts on larger screens.

plurals
  1. POjavascript dynamic regex vs hard typed and captures
    primarykey
    data
    text
    <p>I've been at this for hours -- I think I need sleep... but no matter how I alter the expression javascript will only capture the 1st and 3rd elements:</p> <pre><code>var number = 09416; var mat = "([0-9]+/[0-9]+/[0-9]+\\s+[0-9]+:[0-9]+)\\s+([A-Z]+)\\s+[0-9,]+\\s+(.*?"+number+".+)"; // month / day / year hour : min AMPM byte size filename containing number in middle var pattern = new RegExp(mat,"gi"); var arr = ['09/07/2010 07:08 PM 1,465,536 BOL09416 BOL31.exe', '09/06/2010 12:13 PM 110,225 BOL09416_BOL030.exe', '09/08/2010 04:46 AM 60,564 BOL09416_BOL32.exe', '09/08/2010 01:08 PM 63,004 bol09416_bol33.exe'] for (var i=0;i&lt;arr.length;i++){ var match = pattern.exec(arr[i]); alert(match); } </code></pre> <p>It is all spaces (no tabs), I've rewriten the regex to be as explainatory as possible... It correctly matches on arr[0] and arr[2], but nulls on the other two. Tried looking for possible typo's, trying different .+,.*,.+? etc. All online matchers show that it should be working: <a href="http://regexr.com?2s44k" rel="nofollow noreferrer">Example</a></p> <p>Anybody have any ideas as to what I'm missing?</p> <p>====================<br> Update:</p> <p>Going through all the awesome suggestions I am stumped even further:</p> <pre><code>var match = arr[i].match(/([0-9]+\/[0-9]+\/[0-9]+\s+[0-9]+:[0-9]+)\s+([A-Z]+)\s+[0-9,]+\s+(.*?09416.+)/g); </code></pre> <p>gives <code>match[0]</code> = full string <code>match[1]</code> = undefined. Basically no captures.</p> <p>where as:</p> <pre><code>var match = /([0-9]+\/[0-9]+\/[0-9]+\s+[0-9]+:[0-9]+)\s+([A-Z]+)\s+[0-9,]+\s+(.*?09416.+)/g.exec(arr[i]); </code></pre> <p>DOES return <code>match[0]</code> = full string, <code>match[1]</code> = date, and so on.</p> <p>So I guess my real question is how to include dynamically made RegExpressions, and have multiple captures? As the only difference between:</p> <pre><code>var number = "09416"; var mat = "([0-9]+/[0-9]+/[0-9]+\\s+[0-9]+:[0-9]+)\\s+([A-Z]+)\\s+[0-9,]+\\s+(.*?09416.+)"; var pattern = new RegExp(mat,'g'); </code></pre> <p>and </p> <pre><code>/([0-9]+\/[0-9]+\/[0-9]+\s+[0-9]+:[0-9]+)\s+([A-Z]+)\s+[0-9,]+\s+(.*?09416.+)/g.exec(arr[i]); </code></pre> <p>is that I hard-typed the number.</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. 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