Assign a PERL style regex in the RegExp constructor?

J

jgarrard

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
 
Z

Zwerfkat

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;

and use the variable later on, like:

string.search(my_reg_exp)

or just:

string.search(reg_expr_array)
 
L

Lee

(e-mail address removed) 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+$/ ];


--
 
J

jgarrard

Lee said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top