Skip to main content
Inspiring
September 15, 2014
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
September 15, 2014

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
September 16, 2014

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

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

Russ

Translate
Inspiring
September 16, 2014

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
September 15, 2014

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