Skip to main content
Inspiring
Sep 15, 2014 4:29 PM UTC
Answered

Correct way to search for character within a string

I am trying to search for the occurrence of a "(" within a text string - unfortunately my code is reported as incorrect.

I extract the text correctly but cannot create the search statement correctly:

var mylistpgftext = pgf.GetText (Constants.FTI_String)   

for (var i=0; i < mylistpgftext.length; i++)             

{                                                        

    var mylistitem = mylistpgftext.sdata              

    var mybracketpos = mylistitem.search(/(/i)    --------------error line-----------------       

    if (mybracketpos != -1)                              

    {                                                    

      alert(mylistitem)                                  

    }                                                    

}                                                       

This topic is closed to new replies. Start a new post to keep the conversation going.
frameexpert
frameexpertCorrect answer
Community Expert
Sep 15, 2014 4:40 PM UTC

Hi Bob, I did not test this, but I think the problem is an unescaped character in your regular expression. Try this.

var mybracketpos = mylistitem.search(/\(/i);

Notice the backslash before the open parens that you are searching for.

-Rick

www.frameexpert.com

3 replies

Legend
Sep 16, 2014 12:39 PM UTC

If a regular expression isn't required, I would think that indexOf would be easier:

var mybracketpos = mylistitem.indexOf("(");

Russ

Translate
Inspiring
Sep 16, 2014 1:19 PM UTC

Hi Russ and Rick,

Finger trouble with Rick reply.

They both work perfectly!!

Many thanks to you both.

Bob

Translate
frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
Sep 15, 2014 4:40 PM UTC

Hi Bob, I did not test this, but I think the problem is an unescaped character in your regular expression. Try this.

var mybracketpos = mylistitem.search(/\(/i);

Notice the backslash before the open parens that you are searching for.

-Rick

Translate
www.frameexpert.com