View Full Version : using regular expression to check the string with parentheses

02-20-2003, 04:38 AM
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.

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.

02-20-2003, 03:03 PM
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:

function reescape(str){
var resp="()?:=[]*+{}^$|/,.!\\"
var found=false
var ret=""
for(var i=0;i<str.length;i++){
for(var j=0;j<resp.length;j++){
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.

02-21-2003, 02:34 AM
Thanks poccil it works, but there's still a problem when i entered a character \.

02-21-2003, 01:59 PM
Modified previous post.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum