Results 1 to 3 of 3
  1. #1

    JavaScript Casesenitive Search

    I'm tryng to make a NON-Casesensitive search and this is what i found:

    PHP Code:
    var match; 
    The problema comes when i try to do this:

    PHP Code:
    var tofind "/find/i";
    This won't work...

    Any ideas why?



  2. #2
    Join Date
    Sep 2002
    var tofind = "/find/i";
    var match =;

    This will work if you are looking for a literal string "/find/i" which I don't think you are looking for. Try this instead.....

    var match =;

    This will find instances of "find" in any case. The slashes are part of the "magic" syntax for regular expressions in Javascript. This is rather unfortunate given the otherwise pristine structure of the language.
    Sam Elsamman
    Advanced Site Builder Software for Web Hosts

  3. #3
    Join Date
    Dec 2005
    The slashes are used to build a regular expression literal; quotes build a string literal. You don't want a string literal because it will look for those characters in your string.

    So if you just leave off the quotes it will work:
    var tofind = /find/i;
    var match =;
    The regular expression literal is really just a shortcut to creating a RegExp object. So if the regular expression needs to be dynamically created based on some other variable, you need to use the full syntax:
    var findstring = "find"; // or assigned somewhere else
    var tofind = new RegExp(findstring, "i");
    var match =;
    I'll leave it as an exercise for the reader to Google for the full RegExp syntax.

Posting Permissions

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