我想验证一个值检查以下格式之一:..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
我不明白为什么我得到这个不正确的行为,对于其他用例似乎工作。
_“我不明白为什么我得到这个不正确的行为” _ - 哪个不正确的行为?你在说什么“1233”的输入? – nnnnnn
是的,这就是我正在谈论的 – gigi
使用反斜杠似乎确实有用。请给它一个答案。 – gigi