469,889 Members | 1,103 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,889 developers. It's quick & easy.

Assign a PERL style regex in the RegExp constructor?

Hi,

I have an array of strings which are regular expressions in the PERL
syntax (ie / / delimeters).

I wish to create a RegExp in order to do some useful work, but am stuck
for a way of getting these strings into the RegExp object.

The RegExp constructor seems to want two parameters - the non /
delimited expression, and the global modifiers.

Am I really going to have to strip off the leading and trailing /
characters or is there a quick way around it?

Many thanks.
Jeremy

Jan 14 '07 #1
3 1409
<jg******@parascan.comwrote in message
news:11**********************@11g2000cwr.googlegro ups.com...
Hi,

I have an array of strings which are regular expressions in the PERL
syntax (ie / / delimeters).

I wish to create a RegExp in order to do some useful work, but am stuck
for a way of getting these strings into the RegExp object.

The RegExp constructor seems to want two parameters - the non /
delimited expression, and the global modifiers.

Am I really going to have to strip off the leading and trailing /
characters or is there a quick way around it?

Many thanks.
Jeremy
You can assign a Reg Expr directly to a variable, like:

var my_reg_exp = /hello/gi;

or in your case something like:

var my_reg_exp = reg_expr_array[i];

and use the variable later on, like:

string.search(my_reg_exp)

or just:

string.search(reg_expr_array[i])



Jan 15 '07 #2
Lee
jg******@parascan.com said:
>
Hi,

I have an array of strings which are regular expressions in the PERL
syntax (ie / / delimeters).

I wish to create a RegExp in order to do some useful work, but am stuck
for a way of getting these strings into the RegExp object.

The RegExp constructor seems to want two parameters - the non /
delimited expression, and the global modifiers.

Am I really going to have to strip off the leading and trailing /
characters or is there a quick way around it?
First of all, what you're calling the PERL syntax actually
predates Perl by about 20 years. What you seem to have is
strings containing RegExp literals.

Simply using string methods to strip off the first and last
slash may not be enough. If your array is populated with
string literals that contain escaped characters (eg, "\s")
as well as the slashes, they're not going to be interpreted
correctly.

The RegExp constructor also takes a RegExp literal instead of
the string. Change your array of strings to an array of RegExp
literals:

var myArray = [ /^\s+[A-Z]/, /^+/, /^\d+$/ ];
--

Jan 15 '07 #3

Lee wrote:
Simply using string methods to strip off the first and last
slash may not be enough. If your array is populated with
string literals that contain escaped characters (eg, "\s")
as well as the slashes, they're not going to be interpreted
correctly.
Urgh! Tell me about it. I'm having to escape for SQL, PHP, HTML
entities and javascript all together. I'm going slowly mad!
The RegExp constructor also takes a RegExp literal instead of
the string. Change your array of strings to an array of RegExp
literals:

var myArray = [ /^\s+[A-Z]/, /^+/, /^\d+$/ ];
Aha! That works a treat! Many many thanks for your help.

Jan 15 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by John Smith | last post: by
77 posts views Thread by Hunn E. Balsiche | last post: by
3 posts views Thread by Alan Pretre | last post: by
reply views Thread by Jean-Marie Gouarné | last post: by
11 posts views Thread by Lord Khaos | last post: by
4 posts views Thread by Er Galv?o Abbott | last post: by
3 posts views Thread by Chris | last post: by
4 posts views Thread by jgabbai | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.