[Javascript] Re: RegExp question
vallinis
vallinis at yahoo.com
Fri Jan 4 13:42:55 CST 2002
The original question was:
-------
I tried that, with... var pattern = new RegExp("/course_/(\d+)_\d+","ig");
...and... var pattern = new RegExp("/course_(\d+)_\d+/","ig"); ...neither
seems to work.
-------
Actually, the problem is as follows:
initialize a new RegExp such as:
pattern = new RegExp()
this is now, obivously, a RegExp object. Therefore it can get avail of all
the built in methods such as compile.
See this strange thing now:
if you have a FORM OBJECT VALUE and you pass it to a compile method as it
may be held by a RegExp object like our 'pattern', doing:
pattern.compile(RegExpInAForm_Field)
it will compile (and, by the way, assigns to pattern itself) no matter what
is in the RegExp as you included it in a form field (yes, there can be
online form fields where you include... regular expressions! I have one at
http://www.unitedscripters.com/spellbinder/greed.html ). I mean that if
you included slashes in the field, the compile method can read them right
(without need of... escaping them).
BUT as soon as you either pass to a compile method or you assign to a
RegExp upon initialization a reg exp string, there we go: neither the
method nor the RegExp are any longer capable of taking care of the slashes
escapeng them authomatically.
All this basically means this: in javaScript a slash followed by a d, which
in regular expressions would mean \d a digit, in between quotes becomes an
escaped d... therefore a string such
"hallo\d"
would be read as:
hallod
and NOT as hallo plus a digit (wehatever that may mean...)
Therefore you have to escape (escape means prepo9ning exactly a backward
slash) all BACKWARD SLASHES you include:
"hallo\\d"
that is, TWO backward slashes.
Let's sum it up:
1) when initalizing a RegExp, and you pass a string either as argument or
to the built in compile method, you do not need the forward slashes
2) but whenever you include a backward slashes, you have to escape it with
another backward slashes
3) step 2 doesn't apply in case you're sending data from a form field or
repeatedly compiling in within a loop.
Enough for an headache I know, anyway I believe this would work:
new RegExp("course_\\d+_\\d+","ig");
May I know ask, by the way, what a string like "course_1_3" would mean?? :o)
I'm just kidding.
have a nice day, ciao
Alberto .·. Vallini
http://www.unitedscripters.com
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
More information about the Javascript
mailing list