Subj : Re: regular expression To : Moe tokrot From : Brendan Eich Date : Tue May 18 2004 04:33 pm Moe tokrot wrote: > Hi, > > I'm trying to use regExp in my application, > It works fine for most patterns but i can't use the character '.' > > > for example: > > char* regExp="var re=new RegExp(\"\\.\",\"g\");re.test(\"ghhfdhgh\");"; > JS_EvaluateScript(cx, global, regExp, strlen(regExp), filename, lineto, > &rval); > > returns me true instead of false !! > > i typed the same pattern (except that i didn't backquote the " characters) > in the shell it works fine > > I 'm stuck... if someone can help ! You really should use const char regExp[] = ..., not char*. You should not use new RegExp here, it requires extra backslash-escaping, which you have failed to include. Read the string from left to right, and from the "outside" in: You gave the C compiler this string: "var re=new RegExp(\"\\.\",\"g\");re.test(\"ghhfdhgh\");" The C compiler turned it into this string, without extra notation on my part: var re=new RegExp("\.","g");re.test("ghhfdhgh"); The JS engine compiles the first string, "\.", into this string: . The JS engine therefore compiles a global regexp to match any character. If you use regular expression literal notation, all is well, and a *lot* more readable: "var re=/\\./g;re.test(\"ghhfdhgh\");" /be .