Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As far as I could tell it wasn't possible to get the offset of a sub-match automatically, you have to do the calculation yourself using either <code>lastIndex</code> of the RegExp, or the <code>index</code> property of the match object returned by <code>exec()</code>. Depending on which you use you'll either have to add or subtract the length of groups leading up to your sub-match. However, this does mean you have to group the first or last part of the Regular Expression, up to the pattern you wish to locate.</p> <p><code>lastIndex</code> only seems to come into play when using the <code>/g/</code> global flag, and it will record the index after the entire match. So if you wish to use <code>lastIndex</code> you'll need to work backwards from the end of your pattern.</p> <p>For more information on the <code>exec()</code> method, see here:</p> <p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec</a></p> <p>The following succinctly shows the solution in operation:</p> <pre><code>var str = '---hello123'; var r = /([a-z]+)([0-9]+)/; var m = r.exec( str ); alert( m.index + m[1].length ); // will give the position of 123 </code></pre> <h3>update</h3> <p>This would apply to your issue using the following:</p> <pre><code>var input = '1234 url( test ) 5678'; var searchedUrl = 'test'; var regexpStr = "(url\\(\\s*)("+searchedUrl+")\\s*\\)"; var regex = new RegExp(regexpStr , 'i'); var match = regex.exec(input); </code></pre> <p>Then to get the submatch offset you can use:</p> <pre><code>match.index + match[1].length </code></pre> <p><code>match[1]</code> now contains <code>url(</code> (plus two spaces) due to the bracket grouping which allows us to tell the internal offset.</p> <h3>update 2</h3> <p>Obviously things are a little more complicated if you have patterns in the RegExp, that you wish to group, before the actual pattern you want to locate. This is just a simple act of adding together each group length.</p> <pre><code>var s = '~- [This may or may not be random|it depends on your perspective] -~'; var r = /(\[)([a-z ]+)(\|)([a-z ]+)(\])/i; var m = r.exec( s ); </code></pre> <p>To get the offset position of <code>it depends on your perspective</code> you would use:</p> <pre><code>m.index + m[1].length + m[2].length + m[3].length; </code></pre> <p>Obviously if you know the RegExp has portions that never change length, you can replace those with hard coded numeric values. However, it's probably best to keep the above <code>.length</code> checks, just in case you &mdash; or someone else &mdash; ever changes what your expression matches.</p>
 

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