Forums > Javascript

String.search problem…

January 23, 2008 | 7:47 pm

I am trying to process the lines of a file using "readline".
I have pasted my code below. For some reason when I call
the method string "search", I get the following error…

• error: js: (null): Javascript SyntaxError: unterminated character class , line 0
• error: js: error calling function Test

For the life of me, I can not figure this one out. I know
search is a method that exists because I have called
it before with no problems. Anyone have any ideas?

// inlets and outlets
inlets = 1;
outlets = 1;

var IniFile = null;
function Test(filename)
{
if(IniFile != null)
{
IniFile.close();
IniFile = null;
}

IniFile = new File(filename, "read");
if(IniFile.isopen == false)
{
post("FileIO Error: could not open file.n");
return;
}
else
post("file open.n");

var OutputStr = "";
IniFile.position = 0;
post("IniFile.position = " + IniFile.position + "n");
post("IniFile.eof = " + IniFile.eof + "n");
var Result = 0;
var Line = "";
while(IniFile.position != IniFile.eof)
{
Line = IniFile.readline (256);
post("input: " + Line + "n");
Result = Line.search("[");
post("search done...n");
if(Result != -1)
{
// remove section delimiters.
Line.replace("[", "");
Line.replace("]", "");
OutputStr += Line + " ";
}
}

outlet(0, OutputStr);
}


January 23, 2008 | 9:34 pm

On 23-janv.-08, at 20:48, Anthony Palomba wrote:

> For the life of me, I can not figure this one out. I know
> search is a method that exists because I have called
> it before with no problems. Anyone have any ideas?

I can’t test your code now, but afaik, String.search() expects a
regular expression as argument, but "[" doesn’t look like a RE.

p

_____________________________
Patrick Delges

Centre de Recherches et de Formation Musicales de Wallonie

http://www.crfmw.be/max


January 23, 2008 | 11:45 pm

String.search takes a string and searches it for a
specified substring. It returns -1 or the index of the
beginning of that substring. I am pretty sure this works.

One difference is that when I normally use it, I pass
a string in from outside. In this case I am using fileobject.getline.


January 23, 2008 | 11:49 pm

On 24 janv. 08, at 00:45, Anthony Palomba wrote:

> String.search takes a string and searches it for a
> specified substring. It returns -1 or the index of the
> beginning of that substring. I am pretty sure this works.

I use it in ej.function, and it works fine.

ej


January 23, 2008 | 11:54 pm

On Jan 23, 2008, at 3:45 PM, Anthony Palomba wrote:

>
> String.search takes a string and searches it for a
> specified substring. It returns -1 or the index of the
> beginning of that substring. I am pretty sure this works.

Your error message would suggest it thinks "[" is the beginning of a
regexp charset. So for ordinary strings maybe this is true since they
are the same as their representation as a regular expression.

Try using "[" and "]" –i.e. backslash to escape the brackets…

Let us know what you find.

-Joshua


January 23, 2008 | 11:57 pm

Btw, the definitive source:

http://developer.mozilla.org/en/docs/
Core_JavaScript_1.5_Reference:Objects:String:search

It is a regular expression. So you definitely need to escape brackets
and the like.

-Joshua


January 24, 2008 | 12:02 am

I was referencing this…

http://www.w3schools.com/jsref/jsref_search.asp

Is this a bad source?


January 24, 2008 | 12:08 am

On Jan 23, 2008, at 4:02 PM, Anthony Palomba wrote:

>
> I was referencing this…
> http://www.w3schools.com/jsref/jsref_search.asp
>
> Is this a bad source?

Well it would appear that this isn’t the way it works in JS 1.5 (may
have been the case in previous versions, or they may be glossing over
regexp behavior). Core Javascript book and mozilla site I sent in my
last message are highly recommended

-Joshua


January 24, 2008 | 4:34 pm

I went ahead and treated it as a regular expression. Line.search("[/[]") does the trick. Thanks for helping
me track this one down.


Viewing 9 posts - 1 through 9 (of 9 total)