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???

4
  • 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. Commented Jan 20, 2015 at 6: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..???? Commented Jan 20, 2015 at 7: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. Commented Jan 20, 2015 at 12:39
  • possible duplicate of Combining regular expressions in Javascript Commented Jan 20, 2015 at 14:46

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

Comments

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(' ; '));

1 Comment

@Stephan: formal grammars have been around long before regexes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.