如何计算reg-ex中的字符长度

时间:2013-04-04 15:34:57

标签: jquery

如何在reg-ex中计算字符长度,现在我正在使用

function CheckForAlphaRegName(theObj) {
if (theObj.value.match(/[^a-zA-Z-\'\s]+/)) {
    theObj.value = theObj.value.replace(/[^a-zA-Z-\'\s]+/g, "");
}
if (theObj.value.match(/[-\'\s]+/).length > 1) {
    theObj.value.match(/[-\'\s]+/g, "");
}

1 个答案:

答案 0 :(得分:0)

对于您拥有的这个特定示例,您需要确保您要查找的字符串与Regexp匹配。因此,只需通过尝试以下代码检查它是否为空

if (theObj.value.match(/[-\'\s]+/)) {
    theObj.value.match(/[-\'\s]+/g, "");
}

如果Obj.value.match(/ [ - \'\ s] + /)返回'null',则表示没有匹配项。希望它有所帮助。