View Full Version : Regular Expresions

Michael B
07-31-2006, 04:55 AM
I am trying to find the indexOf searchText(a var from user input) in the string textbox(also a var). I want it case insentive so I added the i flag.

Here is what I am trying
textbox.indexOf(/searchText/i, startPos);

It never finds the string in any of tests I have done. What am I doing wrong?

07-31-2006, 05:52 AM
it won't work because you've got values in there the indexOf method doesn't understand. indexOf is always case sensitive, you can't turn it on or off. and your search string has to be in quotations you can't do /whatever/. if you wanna use something that will return the same results, i.e.: if you use indexOf it will either return the position of the first character or -1 if it doesn't exist, you can use the search method it will do the same thing, so you could do something like:

that will search for the string without being case sensitive.

Michael B
07-31-2006, 07:14 AM
But then I can't use startPos :(

oh well I supose I will have to cut the thing up with substring or what ever and search the substring.

textbox.search(/searchText/i) searches for searchText and not the value of the var called searchText, how do I do that?

07-31-2006, 03:17 PM
Assuming the searchText is being passed i as a string of a regular expression to use, first create a regular expression of the string and then apply the regular expression in the reg. ex. method you wish to use...

// regEx = new RegExp(string of reg. ex., string of flags) so:
var myRegEx = new RegExp(searchText, "i");

Check out visibone's site (http://www.visibone.com/regular-expressions/) for a good reg. ex. cheat sheet.

Michael B
08-01-2006, 02:43 AM
I don't like these Regular Expresions but I am going to have to get a lot better at them if I want to acheive my aim.

Thanks for the advice and cheat sheet.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum