0

Possible Duplicate:
How can I concatenate regex literals in Javascript?

I have two regulars expressions in my hand. A and B what i want to do is that, my expression should start with A and then finish with B how can i do that.

A=[^a-zA-Z] and B=/.+/;

Community
  • 1
  • 1

1 Answers1

0

/* There are a lot of ways to go wrong with regular expressions- this method ignores any flags in the first expression, and doesn't check for errors- It is safer to write your regular expressions whole, or build them up from strings. */

function mergeRx(start, end){
    start= String(start).substring(1);
    end= String(end).substring(1);
    var rx=/\/([igm]+)?$/,
    flag= end.match(/[igm]+$/) || '';

    start= start.replace(rx, '')+end.replace(rx, '');
    return RegExp(start, flag);
}

var A=/[^a-zA-Z]/, B=/.+/;
mergeRx(A, B)

/*  returned value: (RegExp)
/[^a-zA-Z].+/
*/
kennebec
  • 102,654
  • 32
  • 106
  • 127