Skip to main content
Inspiring
September 15, 2014
Answered

Correct way to search for character within a string

  • September 15, 2014
  • 2 replies
  • 452 views

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 has been closed for replies.
Correct answer frameexpert

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

2 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

Inspiring
September 16, 2014

Hi Russ and Rick,

Finger trouble with Rick reply.

They both work perfectly!!

Many thanks to you both.

Bob

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

www.frameexpert.com