使用正则表达式匹配这个模式 - 5h 到 6h,4am 到 9am,3pm 到 8pm,4h 到 9pm......
我已经有了正则表达式 - 5h, 4pm, 5am ......
我想在凌晨 4 点到 9 点之间使用这个正则表达式。
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)
这个 reg_expr 匹配像 4h,5pm,8am 这样的模式......
这个变量在凌晨 4 点到 9 点的比赛中使用,
like var reg_data = ((reg_expr)(/\s(to)(?=\s)/gi)(reg_expr))...
这可能吗??? 如果是那么怎么办???
最佳答案
Is it possible.???
是
if yes then how???
您可以通过调用 #toString
方法或使用 source
属性来获取 RegExp 对象的字符串表示形式。
此外,您可以从字符串创建一个 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);
关于javascript - 在 javascript 的正则表达式中使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28038800/