Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching String 1

Status
Not open for further replies.

TheConeHead

Programmer
Aug 14, 2002
2,106
US
I want to be able to search something like:

var holder = "123456";

to find out if a character is there, like "3"

I tried holder.search(/3/i) but that just returns where it is... I just want like a true/false returned if the character is found in the string....

[conehead]
 
function inStr(szStr,szChar)
{
return (szStr.indexOf(szChar) != -1) ? true : false;
}

alert(inStr("123456","3"));

alert(inStr("123456","8"));
 
try this:

(/[3]/).test(holder)

-kaht

banghead.gif
 

You could just use
Code:
search()
as you were at the start, Conehead.

Like:
Code:
if( holder.search(/3/i) != -1 )
. Though it's not a bad idea to tidy it up into a function.
 
I think kaht's solution is best, you were close with the reg exp.
Code:
/3/.test(holder) //returns true if 3 is in holder

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
I tested /[3]/.test(holder) and it worked too, I'm guessing the []'s are so that you can provide multiple chars?

-kaht

banghead.gif
 
Sorry " you were close with the reg exp." - the you was meant at TheConeHead and his original reg exp attempt.

You are right about the [], if you put in [35] it would mean a 3 or a 5.

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top