我一直在玩Dominion Land Surveyor(DLS)位置的正则表达式。我希望在html输入文本框的keyup事件上强制执行此格式,因此我尝试通过jQuery和javascript执行此操作。
格式为:xx-xx-xxx-xxWx
其中
- 前两个x可以在01和16之间
- 破折号是一个破折号
- 接下来的两个x可以在01到36之间
- 接下来的三个x可以在001到127之间
- 接下来的两个x可以在01到34之间
- W是Wt
- 最后的x可以在1-8之间
使用regexpal.com,我能够确认表达式:
^(0[1-9]|1[0-6])[/-](0[1-9]|1[0-9]|2[0-9]|3[0-6])[/-](0[0-9][1-9]|1[0-1][0-9]|1[2][0-7])[/-](0[1-9]|1[0-9]|2[0-9]|3[0-4])[W][1-8]$
这可以解决我希望它如何工作的问题,但在尝试将其集成到我的jQuery替换方法时,我无法克服语法错误。我只修改了正则表达式,包括一个起始和结束正斜杠。
这是我试图在keyup事件上执行的方法:
function LsdLocation(obj) {
var value = obj.val();
var regex = /^(0[1-9]|1[0-6])[/-](0[1-9]|1[0-9]|2[0-9]|3[0-6])[/-](0[0-9][1-9]|1[0-1][0-9]|1[2][0-7])[/-](0[1-9]|1[0-9]|2[0-9]|3[0-4])[W][1-8]$/;
value = value.replace(regex, '');
obj.val(value);
}
非常感谢任何帮助!
答案 0 :(得分:0)
我不确定,但是关键事件看起来部分匹配是有效的 接受后,整个表单必须作为完整的验证正则表达式存在。
它挑剔,但边缘条件(部分匹配)遵循一个简单的概念
逐步(滚动)可选匹配序列,始终由EOS终止,
字符串的结尾。每个可选段必须将其第一层(字符)作为
非可选。
在解析正则表达式时,似乎可能是一个递归算法 (如果你开发那些东西)。
这是一个例子:
编辑 - 这个正则表达式可能看起来很大,但它的每秒执行速度比键盘电子软件缓冲区快得多。
# /^(0(?:[1-9]|$)|1(?:[0-6]|$))(?:-(?:(0(?:[1-9]|$)|[12](?:[0-9]|$)|3(?:[0-6]|$))(?:-(?:(0(?:[0-9](?:[1-9]|$))?|1(?:[0-1](?:[0-9]|$))?|1(?:[2](?:[0-7]|$))?)(?:-(?:(0(?:[1-9]|$)|[12](?:[0-9]|$)|3(?:[0-4]|$))(?:W(?:[1-8]|$))?)?)?)?)?)?)?$/
^ # BOS - Key pressed, if its a printable character, has to be 0 or 1 here
(
0
(?: [1-9] | $ ) # EOS
| 1
(?: [0-6] | $ ) # EOS
)
(?: # The rest are optional
-
(?:
(
0
(?: [1-9] | $ ) # EOS
| [12]
(?: [0-9] | $ ) # EOS
| 3
(?: [0-6] | $ ) # EOS
)
(?:
-
(?:
(
0
(?:
[0-9]
(?: [1-9] | $ ) # EOS
)?
| 1
(?:
[0-1]
(?: [0-9] | $ ) # EOS
)?
| 1
(?:
[2]
(?: [0-7] | $ ) # EOS
)?
)
(?:
-
(?:
(
0
(?: [1-9] | $ ) # EOS
| [12]
(?: [0-9] | $ ) # EOS
| 3
(?: [0-4] | $ ) # EOS
)
(?:
W
(?: [1-8] | $ ) # EOS
)?
)?
)?
)?
)?
)?
)?
$ # EOS
答案 1 :(得分:0)
这是一个自包含的答案(非jquery)。
请参阅代码中的注释。
它不会清除输入值。
相反,当用户接近输入有效数据时,它会提供背景颜色反馈。
以下代码也可以DominionLandSurveyor.js
的形式提供感谢@sln提出的增量正则表达式。
// My answer for
// http://stackoverflow.com/questions/20507396/regular-expression-for-dominion-land-surveyor/20508597?noredirect=1#comment30663755_20508597
// Use it in Firefox Scratchpad or Google Chrome devtools Snippets.
/* jslint browser: true */
/*global console: false*/
'use strict';
(function testDominionLandSurveyor() {
var inp = document.body.appendChild(document.createElement('input'));
inp.type = 'text';
inp.value = '01-01-001-01W1';
inp.title = 'e.g. 16-36-127-34W8';
inp.onkeyup = function LsdLocation(obj) {
var value = obj.target.value;
// Based on idea by sln in http://stackoverflow.com/a/20508410/743358
// Match incrementally, producing ever more subexpression matches until fully matched.
var regex = /^(?:(0(?:[1-9])?|1(?:[0-6])?)(?:-(?:(0(?:[1-9])?|1(?:[0-9])?|2(?:[0-9])?|3(?:[0-6])?)(?:-(?:(0(?:[0-9](?:[1-9])?)?|1(?:[0-1](?:[0-9])?)?|1(?:[2](?:[0-7])?)?)(?:-(?:(0(?:[1-9])?|1(?:[0-9])?|2(?:[0-9])?|3(?:[0-4])?)(?:W(?:([1-8]))?)?)?)?)?)?)?)?)$/;
var matches = regex.exec(value);
console.log(matches);
if (matches) {
var unmatched = matches.filter(function(value) {
return value === undefined;
});
console.log(unmatched);
obj.target.style.backgroundColor = 'hsl(360,100%,'
// Increase luminance with each component match, up to white.
+ (100 - 8 * unmatched.length) + '%)';
console.log(obj.target.style.backgroundColor);
} else {
obj.target.style.backgroundColor = 'red';
}
// obj.target.value = value;
};
})();
答案 2 :(得分:0)
我想出了这个正则表达式。可能对您的项目有用。 如果您有任何困惑,请告诉我。请点击链接
var regex = /^((?:([S][E])|(?:[S][W])|(?:[N][W])|(?:[N][E]))(?:(0(?:[1-9])?|1(?:[0-9])?))(?:-(?:(0(?:[1-9])?|1(?:[0-9])?|2(?:[0-9])?|3(?:[0-6])?)))(?:-(?:(0(?:[1-9])?|1(?:[0-9])?|2(?:[0-9])?|3(?:[0-6])?)))(?:-(?:W(?:(0([1-8]))|[1-8])?|((0([1-8]))|[1-8])?)?)?)$/;