我想验证一个值是否检查以下格式之一:..A , B.. , A..B
并检索值:(null, A) , (B, null) , (A, B)
这是我的代码:
var regexRange = new RegExp("^([a-zA-Z0-9]+)\.\.([a-zA-Z0-9]*)$|^[a-zA-Z0-9]*\.\.([a-zA-Z0-9]+)$");
function getRangeValues(value) {
var from = null;
var to = null;
var matches = regexRange.exec(value);
if (matches !== null) {
if (matches[3] !== undefined) {
to = matches[3];
}
else if(matches[1]!==undefined && matches[1]!=='') {
from = matches[1];
if (matches[2] !== undefined && matches[2] !== '') {
to = matches[2];
}
}
}
var range = { From: from, To: to };
return range;
}
Value: 1233 => From=12, To=null
我不明白为什么我会得到这种不正确的行为,对于其他用例它似乎有效。