Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>You don't need the index.</h2> <p>This is a case where providing just a bit more information would have gotten a much better answer. I can't fault you for it; we're encouraged to create simple test cases and cut out irrelevant detail.</p> <p>But one important item was missing: what you plan to <em>do</em> with that index. In the meantime, we were all chasing the wrong problem. :-)</p> <p>I had a feeling something was missing; that's why I asked you about it.</p> <p>As you mentioned in the comment, you want to find the URL in the input string and highlight it in some way, perhaps by wrapping it in a <code>&lt;b&gt;&lt;/b&gt;</code> tag or the like:</p> <pre><code>'1234 url( &lt;b&gt;test&lt;/b&gt; ) 5678' </code></pre> <p>(Let me know if you meant something else by "highlight".)</p> <p>You can use character indexes to do that, however there is a much easier way using the regular expression itself.</p> <h3>Getting the index</h3> <p>But since you asked, if you <em>did</em> need the index, you could get it with code like this:</p> <pre><code>var input = '1234 url( test ) 5678'; var url = 'test'; var regexpStr = "^(.*url\\(\\s*)"+ url +"\\s*\\)"; var regex = new RegExp( regexpStr , 'i' ); var match = input.match( regex ); var start = match[1].length; </code></pre> <p>This is a bit simpler than the code in the other answers, but any of them would work equally well. This approach works by anchoring the regex to the beginning of the string with <code>^</code> and putting all the characters before the URL in a group with <code>()</code>. The length of that group string, <code>match[1]</code>, is your index.</p> <h3>Slicing and dicing</h3> <p>Once you know the starting index of <code>test</code> in your string, you could use <code>.slice()</code> or other string methods to cut up the string and insert the tags, perhaps with code something like this:</p> <pre><code>// Wrap url in &lt;b&gt;&lt;/b&gt; tag by slicing and pasting strings var output = input.slice( 0, start ) + '&lt;b&gt;' + url + '&lt;/b&gt;' + input.slice( start + url.length ); console.log( output ); </code></pre> <p>That will certainly work, but it is really doing things the hard way.</p> <p>Also, I left out some error handling code. What if there is no matching URL? <code>match</code> will be <code>undefined</code> and the <code>match[1]</code> will fail. But instead of worrying about that, let's see how we can do it without any character indexing at all.</p> <h2>The easy way</h2> <p>Let the regular expression do the work for you. Here's the whole thing:</p> <pre><code>var input = '1234 url( test ) 5678'; var url = 'test'; var regexpStr = "(url\\(\\s*)(" + url + ")(\\s*\\))"; var regex = new RegExp( regexpStr , 'i' ); var output = input.replace( regex, "$1&lt;b&gt;$2&lt;/b&gt;$3" ); console.log( output ); </code></pre> <p>This code has three groups in the regular expression, one to capture the URL itself, with groups before and after the URL to capture the other matching text so we don't lose it. Then a simple <code>.replace()</code> and you're done!</p> <p>You don't have to worry about any string lengths or indexes this way. And the code works cleanly if the URL isn't found: it returns the input string unchanged.</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. VO
      singulars
      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