这个jQuery验证代码有什么问题? regexp.exec(值) [英] Whats wrong with this jQuery validation code? regexp.exec(value)

查看:53
本文介绍了这个jQuery验证代码有什么问题? regexp.exec(值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

值将为任何值,并且matchs为null.这样做的目的是拆分一个字符串,例如"1991-12-01",并确保该字符串的所有部分都是有效日期.

Value will be anything and matches is null. The point of this is to split up a string like "1991-12-01" and make sure that all of the parts of the string are valid dates.

dateISO: function(value, element) {
    if (this.optional(element)) return true;
    var regexp = new RegExp('^\d{4}[\/-](\d{1,2})[\/-](\d{1,2})$');
    var matches = regexp.exec(value);
    alert(matches);

有什么想法吗?

推荐答案

您提供的表达式是字符串,因此需要转义:

The expression you're giving is a string, thus, needs escaping:

var regexp = new RegExp('^\\d{4}[\\/-](\\d{1,2})[\\/-](\\d{1,2})$');

或者,您可以执行perl样式的表达式,但是需要转义斜杠:

Alternatively, you can do the perl-styled expressions, but slashes need to be escaped:

var regexp = /^\d{4}[\\/-](\d{1,2})[\\/-](\d{1,2})$/;

(perl样式的正则表达式返回RegExp对象)

(The perl-styled regular expression returns a RegExp object)

这篇关于这个jQuery验证代码有什么问题? regexp.exec(值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆