我正在创建一个用于项目长度的输入掩码。此输入将在模糊时转换为“正确”格式,但它应该接受数字、小数、空格、单引号、双引号和 / 表示分数(无字母)。
我有一个好的开始,但我不是正则表达式大师,我觉得我的模式太复杂了。因此允许以下值:
5 6(英尺和英寸用空格隔开)
5'6"(正确格式的英尺和英寸)
5.2 6(以空格分隔的小数英尺)
5.2'6"(正确格式的小数英尺)
5 6.1(以空格分隔的十进制英寸)
5'6.1"(正确格式的十进制英寸)
5.2 6.1(以空格分隔的十进制英尺和英寸)
5.2'6.1"(正确格式的十进制英尺和英寸)
5 6 1/2(以上任意组合后跟空格和分数)
5.2'6.1 1/2"(同样带小数)
78 英寸(仅英寸)
78.4"(仅带小数的英寸)
比较挑剔,我知道。我有一些正在进行中的工作,我已经将其分解为更具可读性(至少对我自己而言)。http://jsfiddle.net/t37m0txu/383/
// allow numbers
var p_num = "[0-9]";
// numbers are up to 9 characters (it needs a range, for some reason)
var p_range = "{0,9}";
// allow a single decimal
var p_dec = "([.]{0,1})";
// allow a single space (needs to happen only if not directly followed by a decimal)
var p_space = "([ ]{0,1})";
// numbers, possible single decimal and/or space
var p_base = p_num + p_range + p_dec + p_space;
// only allow a single/double quote after a number
var p_afternum = "?(?=" + p_num + ")";
// allow a single or double quote
var p_quote = "(\'(0?" + p_base + ")?\|\"$)";
// issues:
// i do not need a range/cap on numbers
// after using decimal or space - only one number is allowed to follow (do not cap the range on numbers, only decimal/space)
// do not allow a space directly following a decimal
// do not allow a decimal directly following a single or double quote
var ex = "(" + p_base + ")" + p_afternum + p_quote + "(0?" + p_base + ")?\""