3

Using regular expression match this pattern - 5h to 6h,4am to 9am,3pm to 8pm,4h to 9pm......

and I have already regular expression for - 5h, 4pm, 5am......

and I want to use this regular expression for match 4am to 9am.

like var reg_expr = (/(\d{1,2}?h$)|(\d{1,2}h(?=\s+))|(\d{1,2}:\d{2}([ap]m)?$)|(\d{1,2}:\d{2}([ap]m)(?=\s+))|(\d{1,2}:\d{2}(?=\s+))|(\d{1,2}([ap]m)?$)|(\d{1,2}([ap]m)(?=\s+))/gi)

this reg_expr matches pattern like 4h,5pm,8am.....

and this variable uses in match 4am to 9am,

like var reg_data = ((reg_expr)(/\s(to)(?=\s)/gi)(reg_expr))...

Is it possible.??? if yes then how???

styvane
  • 59,869
  • 19
  • 150
  • 156
Bhumi Patel
  • 35
  • 1
  • 1
  • 6
  • 4
    You can't combine two regular expression objects directly. You can take the two strings that specify regular expressions and combine those two strings and then do `var regex = new RegExp(str1 + str2)` or even `var regex = new RegExp("(" + str1 + ")" + str2)`. So, basically build the regex string using string manipulation from your other strings variables and then create one new regex from that combined string. – jfriend00 Jan 20 '15 at 06:14
  • str1 = "pattern(/s+)" msg = "pattern pattern matching pattern pattern." var re = new RegExp(str1, "g"); var temp = msg.match(re); In this case temp is null...because i add /s+ , how to add space after pattern..???? – Bhumi Patel Jan 20 '15 at 07:30
  • If you want to pull the times out as distinct values, then first split the string on commas and then on "to" 's, including optional spaces in each split. – bloodyKnuckles Jan 20 '15 at 12:39
  • possible duplicate of [Combining regular expressions in Javascript](http://stackoverflow.com/questions/9213237/combining-regular-expressions-in-javascript) – Stephan Jan 20 '15 at 14:46

2 Answers2

2

Is it possible.???

Yes

if yes then how???

You can get the string representation of a RegExp object by calling the #toString method or using the source property.

Moreover, you can create a RegExp object from a string.

var reg_expr = /(\d{1,2}?h$)|(\d{1,2}h(?=\s+))|(\d{1,2}:\d{2}([ap]m)?$)|(\d{1,2}:\d{2}([ap]m)(?=\s+))|(\d{1,2}:\d{2}(?=\s+))|(\d{1,2}([ap]m)?$)|(\d{1,2}([ap]m)(?=\s+))/;

var reg_data = new RegExp(
    reg_expr.source + 
    /\s(to)(?=\s)/.source +
    reg_expr.source,
    'gi'
);

alert(reg_data.source);
Stephan
  • 41,764
  • 65
  • 238
  • 329
1

If your regex is complex and becomes unreadable at some point, you might consider the grammar approach. A grammar can be declared as an object, with symbols as keys and corresponding productions as values. Productions are actually just regexes, but with special syntax for symbols (like @symbol) and whitespace for readability. When you need a regex for a symbol, you (trivially) create it on the fly:

// grammar for time intervals

times = {
    'space'   : '\\s+',
    'digit'   : '\\d',
    'hours'   : '@digit @digit?',
    'minutes' : ': @digit @digit',
    'suffix'  : 'h | am | pm',
    'time'    : '@hours @minutes? @suffix?',
    'interval': '@time @space to @space @time'
};

// convert a grammar rule to regex

function regex(grammar, symbol, flags) {

    function expand(s) {
        return s.replace(/@(\w+)/g, function(_, $1) {
            return '(?:' + expand(grammar[$1]) + ')';
        });
    }

    return new RegExp(
        expand(grammar[symbol]).replace(/\s+/g, ''),
        flags);
}

// test!

interval = regex(times, 'interval', 'g');
test = '5h to 6h,4am to 9am,3pm to 8pm,4h to 9pm';
document.write(test.match(interval).join(' ; '));
georg
  • 211,518
  • 52
  • 313
  • 390