【问题标题】:Unexpected '\d' in JSLintJSLint 中出现意外的“\d”
【发布时间】:2012-11-03 10:19:34
【问题描述】:

只是一个小问题: 我试图让我的代码 JSLint 没有错误,但遇到了这个问题:

意外的“\d”。 (在两个正则表达式中)

hourduration = parseInt(activity.endTime.replace(":\d\d", ""), 10) - parseInt(activity.startTime.replace(":00", ""), 10);

minuteduration = (parseInt(activity.endTime.replace("(\d)?\d:", ""), 10) - parseInt(activity.startTime.replace(":00", ""), 10)) / 60;

我可以做些什么来改进我的正则表达式,以便 jslint 对其进行验证?

谢谢!

解决方案:

hourduration = parseInt(activity.endTime.replace(/:\d\d/, ""), 10) - parseInt(activity.startTime.replace(":00", ""), 10);

minuteduration = (parseInt(activity.endTime.replace(/(\d)?\d:/, ""), 10) - parseInt(activity.startTime.replace(":00", ""), 10)) / 60;

【问题讨论】:

  • 我怀疑你正则表达式的有效性。即使是这样,它也被无法转换为任何整数值的空字符串替换。

标签: javascript regex jslint


【解决方案1】:

JSLint 希望您使用 RegExp 对象或 /regex/ 'string',而不是普通字符串:

// JSLint error
foo.match(':\d\d');
foo.match(RegExp(':\\d\\d'));

// no error
foo.match(/:\d\d/);
foo.match(new RegExp(':\\d\\d'));

编辑:所有示例都是有效的,但最后两个是使用正则表达式的官方方式。

【讨论】:

    猜你喜欢
    • 2012-08-13
    • 2012-10-21
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 2012-02-29
    • 2017-05-22
    • 1970-01-01
    • 2012-06-23
    相关资源
    最近更新 更多