Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Regular Expresions

    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
    Code:
    textbox.indexOf(/searchText/i, startPos);
    It never finds the string in any of tests I have done. What am I doing wrong?

  • #2
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    textbox.search(/search_string/i)
    that will search for the string without being case sensitive.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #3
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    EDIT:
    Code:
    textbox.search(/searchText/i)
    searches for searchText and not the value of the var called searchText, how do I do that?
    Last edited by Michael B; 07-31-2006 at 07:26 AM.

  • #4
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

    Code:
    // regEx = new RegExp(string of reg. ex., string of flags) so:
    var myRegEx = new RegExp(searchText, "i");
    textbox.search(myRegEx);
    Check out visibone's site for a good reg. ex. cheat sheet.
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!

  • #5
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •