Using regular expression match this pattern - 5h to 6h,4am to 9am,3pm to 8pm,4h to 9pm......
使用正则表达式匹配这个模式——5h到6h,4am到9am,3pm到8pm,4h到9pm
and I have already regular expression for - 5h, 4pm, 5am......
我已经有了- 5h, 4pm, 5am的正则表达式
and I want to use this regular expression for match 4am to 9am.
我想用这个正则表达式来表示match 4am到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.....
这个reg_expr匹配的模式是4h,5pm,8am…
and this variable uses in match 4am to 9am,
这个变量在匹配早上4点到9点时使用,
like var reg_data = ((reg_expr)(/\s(to)(?=\s)/gi)(reg_expr))...
Is it possible.??? if yes then how???
它是可能的。? ? ?如果是的,那么如何? ? ?
2 个解决方案
#1
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.
您可以通过调用#toString方法或使用source属性获得RegExp对象的字符串表示形式。
Moreover, you can create a RegExp
object from a string.
此外,还可以从字符串创建RegExp对象。
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);
#2
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:
如果您的regex很复杂,并且在某些时候变得不可读,您可以考虑使用语法方法。语法可以声明为对象,符号作为键,相应的结果作为值。产品实际上只是regexes,但是具有符号的特殊语法(如@symbol)和可读性的空格。当您需要一个regex作为一个符号时,您(通常)会动态地创建它:
// 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
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.
您可以通过调用#toString方法或使用source属性获得RegExp对象的字符串表示形式。
Moreover, you can create a RegExp
object from a string.
此外,还可以从字符串创建RegExp对象。
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);
#2
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:
如果您的regex很复杂,并且在某些时候变得不可读,您可以考虑使用语法方法。语法可以声明为对象,符号作为键,相应的结果作为值。产品实际上只是regexes,但是具有符号的特殊语法(如@symbol)和可读性的空格。当您需要一个regex作为一个符号时,您(通常)会动态地创建它:
// 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(' ; '));