1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

indexOf - how does it work?

Discussion in 'Software Development' started by Mr Red, Apr 19, 2010.

Thread Status:
Not open for further replies.
  1. Mr Red

    Mr Red Thread Starter

    Dec 1, 2008
    I have been trying to get the index number of text in a string and indexOf seemed the most logical. And the easiest.
    but I just get a returned value of -1. I am looking for values held inside TAGs and have a solution but it annoys me that I can't get indexOf to work. When string appears to be a similar function and does work (it finds a value of 5 and alert reports the correct string).

    function  foo(ar,n){ 
     var id = /id/g; 
     var gt = />/g; 
     var lt = /</g; 
     var kk = ar[1].toString(); 
     var kkm = kk.indexOf(id)+1; 
     var kkl = kk.search(id); 
     kkl = kkl+kk.slice(kkl).search(gt)+1; 
     var ccm =  ar[1].toString().indexOf(lt)-1; 
     var ccl =  kkl+kk.slice(kkl).search(lt); 
     alert("the first date I find  is " +kk.slice(kkl,ccl) ); 
    // non-relevant code removed
    Any Ideas?

  2. andythepandy


    Jul 2, 2006
    The indexOf function doesn't support regular expressions. Instead you can use the search function which returns, as indexOf does, the position of the search string. Try this:
    var id = /id/g; 
    var gt = />/g; 
    var lt = /</g; 
    var kk = "where is id?'"; 
    var kkm = kk.search(id); 
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/917837

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice