| strange syntax error with bracket character only
• | | | |
 | strange syntax error with bracket character only
by Daniel Crooks on Apr 27, 2011 at 6:49:09 am |
hi all
I am having a very strange problem with a script I'm writing.
I'm doing a string search and get a syntax error only if I search for an opening bracket character i.e. "(" . Any other character including the closing bracket, various other symbols, chunks of text works fine...? but any string containing the opening bracket reports an error.
is this a bug ? it's driving me crazy .
doesn't work:
else if (lineData.search(")") != -1){
else if (lineData.search("blah ) blah") != -1){
this does:
else if (lineData.search("(") != -1){
else if (lineData.search("#blah") != -1){
../daniel
| | | | |
• | | | |  | Re: strange syntax error with bracket character only by Dan Ebberts on Apr 27, 2011 at 2:24:22 pm |
I can't get it to fail.
This works for me:
lineData ="67890()-=";
if (lineData.search(")") != -1) alert ("yay") else alert ("boo");
Dan
| | | | |
• | | | |  | Re: strange syntax error with bracket character only by Daniel Crooks on Apr 28, 2011 at 12:20:10 am |
Dan,
thanks for checking, I can confirm the closing bracket works but try searching for an opening bracket... "(" like this...
lineData ="67890()-=";
if (lineData.search("(") != -1) alert ("yay") else alert ("boo");
I get "unable to execute script at line 2. Syntax error"
???
cheers../daniel
| | | | |
• | | | |  | Re: strange syntax error with bracket character only by Dan Ebberts on Apr 28, 2011 at 12:31:16 am |
Ah yes. indexOf() works, like this:
lineData ="67890()-=";
if (lineData.indexOf("(") != -1) alert ("yay") else alert ("boo");
But search() takes a regular expression as a parameter, so you probably have to escape the paren, like this:
lineData ="67890()-=";
if (lineData.search(/\(/) != -1) alert ("yay") else alert ("boo");
Dan
| | | | |
• | | | |  | Re: strange syntax error with bracket character only by Daniel Crooks on Apr 28, 2011 at 12:47:46 am |
still no joy !
I thought perhaps there was some kind of escape needed but no, even with the \ in there I still get the syntax error, swap out the ( for an other character and it works... bizarre !?
| | | | |
• | | | |  | Re: strange syntax error with bracket character only by Dan Ebberts on Apr 28, 2011 at 2:21:39 am |
Did you try the forward slashes instead of the quotes?
Dan
| | | | |
• | | | |  | Re: strange syntax error with bracket character only by Daniel Crooks on Apr 28, 2011 at 4:42:22 am |
sorry, my bad. I copied the code from the email not the cow site... doh.
the (/\(/) worked !!! (even if it does look like ascii art !)
thanks, Dan
but I'm still not sure why the regular expression couldn't handle the "(" when it could handle everything else ?
| | | | |
• | | | |  | Re: strange syntax error with bracket character only by Dan Ebberts on Apr 28, 2011 at 5:24:26 am |
Inside a RegExp (like search() and match() use) you should escape (back slash) any of these regexp special characters if you want to match them as literals:
.|*?+(){}[]^$\
Dan
| | | | |
• | | | |  | Re: strange syntax error with bracket character only by Daniel Crooks on Apr 28, 2011 at 6:01:36 am |
thanks Dan, really appreciate it !
| | | | |
| |
|