Jump to content

search string not working with variable reg express


tkm

Recommended Posts

Hello mates,

  I am trying to use javascript search method to look for a string. Here is my code:

 

for (;i<100;i++)
{
var search_txt=el.getValue();
var rg = new RegExp(search_txt,'i');
var cname=record.data.company_name;
if (cname.search(rg)>=0)
return true;
}

 

But my above code is not working. Can someone kindly point out what I am doing wrong. It will be a great help. Thank you.

Hello mates,

  I am trying to use javascript search method to look for a string. Here is my code:

 

for (;i<100;i++)
{
var search_txt=el.getValue();
var rg = new RegExp(search_txt,'i');
var cname=record.data.company_name;
if (cname.search(rg)>=0)
return true;
}

 

But my above code is not working. Can someone kindly point out what I am doing wrong. It will be a great help. Thank you.

 

You should probably show some more code.  Where does 'el' come from?  What does the getValue() function do?  Do you declare 'i' anywhere?  What do you mean by it 'not working'?  Is it returning the wrong string?  No string?  Is it iterating through all 100 elements without exiting when it should find the right one?

That code makes no sense to me, so I can't determine what it's supposed to do.

 

1. Where is the initial value of i defined?

2. The regular expression is crafted to search for the string 'i' not the variable i; so what is the purpose of the loop? It's just checking the same string over and over for the same search value

3. The code will return true if the search value is found. But, I see no "return false" if it does not.

 

Please explain what you are wanting the code to do along with some sample input and outputs you would expect to use.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.