Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions strange syntax error with bracket character only

  • strange syntax error with bracket character only

    Posted by Daniel Crooks on April 27, 2011 at 6:49 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

    Daniel Crooks replied 15 years ago 2 Members · 8 Replies
  • 8 Replies
  • Dan Ebberts

    April 27, 2011 at 2:24 pm

    I can’t get it to fail.

    This works for me:

    lineData =”67890()-=”;
    if (lineData.search(“)”) != -1) alert (“yay”) else alert (“boo”);

    Dan

  • Daniel Crooks

    April 28, 2011 at 12:20 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

  • Dan Ebberts

    April 28, 2011 at 12:31 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

  • Daniel Crooks

    April 28, 2011 at 12:47 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 !?

  • Dan Ebberts

    April 28, 2011 at 2:21 am

    Did you try the forward slashes instead of the quotes?

    Dan

  • Daniel Crooks

    April 28, 2011 at 4:42 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 ?

  • Dan Ebberts

    April 28, 2011 at 5:24 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

  • Daniel Crooks

    April 28, 2011 at 6:01 am

    thanks Dan, really appreciate it !

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy