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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2003
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    using regular expression to check the string with parentheses

    Hi!
    I'm having problem in using regular exression when a user entered a string with parentheses. I want the regexp to accept what ever the user entered.

    I used this to my incremental search.
    e.g.


    srcstring = 'sample (x)' // string entered by user

    var regExpr = eval("/^" + srcString + "/"); /*regular expression pattern*/


    selElementText.search(regExpr) /* search the string in select element*/

    here's the error: expected ')' character in regular expresssion.

  • #2
    New Coder
    Join Date
    Jul 2002
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The following characters are special in regular expressions and need to be escaped with a backslash:

    ( ) ? : = [ ] * + { } ^ $ | / , . ! \

    This is an escaping function for regular expression strings:

    Code:
    function reescape(str){
     var resp="()?:=[]*+{}^$|/,.!\\"
     var found=false
     var ret=""
     for(var i=0;i<str.length;i++){
      found=false
      for(var j=0;j<resp.length;j++){
       if(str.charAt(i)==resp.charAt(j))
        {found=true;break}
      }
      if(found)ret+="\\"
      ret+=str.charAt(i)
     }
     return ret
    }
    With this function, you can safely escape the string:

    var regExpr = eval("/^" + reescape(srcString) + "/");

    For your particular application, do this instead:

    selElementText.indexOf(srcString) // checks if the string exists

    By the way, srcstring is NOT the same as srcString, when it comes to case sensitivity.
    Last edited by poccil; 02-21-2003 at 02:56 PM.

  • #3
    New to the CF scene
    Join Date
    Feb 2003
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks poccil it works, but there's still a problem when i entered a character \.

  • #4
    New Coder
    Join Date
    Jul 2002
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Modified previous post.


  •  

    Posting Permissions

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