views:

1373

answers:

5

Is it possible to do something like this?

var pattern = /some regex segment/ + /* comment here */
    /another segment/;

Or do I have to use new RegExp() syntax and concatenate a string? I'd prefer to use the literal as the code is both more self-evident and concise.

+1  A: 

You'll have to use new RegExp !-)

roenving
+11  A: 

Here is how to create a regular expression without using the regular expression literal syntax. This lets you do arbitary string manipulation before it becomes a regular expression object:

var segment_part = "some bit of the regexp";
var pattern = new RegExp("some regex segment" + /*comment here */
              segment_part + /* that was defined just now */
              "another segment");

If you have two regular expression literals, you can in fact concatenate them using this technique:

var expression_one = /foo/;
var expression_two = /bar/;
var expression_three = new RegExp(expression_one.source + expression_two.source);

It's not entirely a good solution, as you lose the flags that were set on expression_one and expression_two, and is more wordy than just having expression one and two being literal strings instead of literal regular expressions.

Jerub
Not to be rude but, I think it was clear that I know that, I was specifically asking if it can be done with the literal.
eyelidlessness
Stackoverflow isn't just for you and me, it's designed to be searched and used as an archive of questions and answers - I gave a proper code example so that someone with a similar question can easily see an appropriate solution.
Jerub
var regex1 = /asdf/;var regex2 = /qwerty/;var regex3 = new RegExp(regex1 + regex2); // /\/asdf\/\/qwerty\//
eyelidlessness
See the '.source' attribute I'm accessing. new RegExp(regex1.source + regex2.source) -> /asdfqwerty/
Jerub
Oof, I missed that. Thanks! That will do.
eyelidlessness
a solution which includes literals:re = new RegExp("pattern1" + "pattern2", ["flags"]) HTH ;-)
Berry Tsakala
Berry, that doesn't include regex literals. That includes escaped RegExp syntax with string literals. o_O
eyelidlessness
A: 

No, the literal way is not supported. You'll have to use RegExp.

Aupajo
A: 

I prefer to use eval('your expression') because it does not add the /on each end/ that ='new RegExp' does.

Praesagus
+1  A: 

I don't quite agree with the "eval" option.

var xxx = /abcd/;
var yyy = /efgh/;
var zzz = new RegExp(eval(xxx)+eval(yyy));

will give "//abcd//efgh//" which is not the intended result.

Using source like

var zzz = new RegExp(xxx.source+yyy.source);

will give "/abcdefgh/" and that is correct.

Logicaly there is no need to EVALUATE, you know your EXPRESSION. You just need its SOURCE or how it is written not necessarely its value. As for the flags, you just need to use the optional argument of RegExp.

In my situation, I do run in the issue of ^ and $ being used in several expression I am trying to concatenate together! Those expressions are grammar filters used accross the program. Now I wan't to use some of them together to handle the case of PREPOSITIONS. I may have to "slice" the sources to remove the starting and ending ^( and/or )$ :) Cheers, Alex.

Alex