indexOf - how does it work?

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Mr Red

Thread Starter
Joined
Dec 1, 2008
Messages
76
Hi
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).

Code:
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?

TIA
 
Joined
Jul 2, 2006
Messages
379
Hi,
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:
Code:
var id = /id/g; 
var gt = />/g; 
var lt = /</g; 
var kk = "where is id?'"; 
var kkm = kk.search(id); 

alert(kkm);
Andy
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Members online

Top