英尺和英寸的正则表达式 - 带小数和分数
Regex for feet and inches - with decimals and fractions
我正在创建用于项目长度的输入掩码。此输入将在模糊时转换为 "proper" 格式,但它应该接受数字、小数、space、单引号、双引号和/(无字母)。
我的开始还不错,但我不是正则表达式大师,我觉得我的模式太复杂了。因此允许以下值:
5 6(英尺和英寸以 spaces 分隔)
5'6"(正确格式的英尺和英寸)
5.2 6(小数点之间用 spaces 分隔)
5.2'6"(正确格式的小数英尺)
5 6.1(以 spaces 分隔的小数英寸)
5'6.1"(正确格式的小数英寸)
5.2 6.1(以 spaces 分隔的小数英尺和英寸)
5.2'6.1"(正确格式的小数英尺和英寸)
5 6 1/2(以上任何组合后跟 space 和分数)
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 + ")?\""
如何只扫描有效的英尺和英寸输入
此扫描结合使用 REGEX 掩码和输入掩码来创建一个强大的、仅限英尺和英寸的文本框。
ex = "[\d]+(?:\.[\d]+|)(?:\s\d+\/\d+|)(?:\s|\'|\\"|)[\d]+(?:\.[\d]+|)(?:\s\d+\/\d+|)(?:\'|\\"|)";
$('#feet').inputmask('Regex', { regex: ex });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/RobinHerbots/jquery.inputmask/3.x/dist/jquery.inputmask.bundle.js"></script>
So the following values are allowed:<br /><br />
5 6 (feet and inches separated by spaces)<br />
5'6" (feet and inches in the correct format)<br />
5.2 6 (decimal feet separated by spaces)<br />
5.2'6" (decimal feet in the correct format)<br />
5 6.1 (decimal inches separated by spaces)<br />
5'6.1" (decimal inches in the correct format)<br />
5.2 6.1 (decimal feet and inches separated by spaces)<br />
5.2'6.1" (decimal feet and inches in the correct format)<br />
5 6 1/2 (any combination above followed by a space and fraction)<br />
5.2'6.1 1/2" (again with decimals)<br />
78" (only inches)<br />
78.4" (only inches with a decimal)<br />
<input id="feet" />
<br />
我正在创建用于项目长度的输入掩码。此输入将在模糊时转换为 "proper" 格式,但它应该接受数字、小数、space、单引号、双引号和/(无字母)。
我的开始还不错,但我不是正则表达式大师,我觉得我的模式太复杂了。因此允许以下值:
5 6(英尺和英寸以 spaces 分隔)
5'6"(正确格式的英尺和英寸)
5.2 6(小数点之间用 spaces 分隔)
5.2'6"(正确格式的小数英尺)
5 6.1(以 spaces 分隔的小数英寸)
5'6.1"(正确格式的小数英寸)
5.2 6.1(以 spaces 分隔的小数英尺和英寸)
5.2'6.1"(正确格式的小数英尺和英寸)
5 6 1/2(以上任何组合后跟 space 和分数)
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 + ")?\""
如何只扫描有效的英尺和英寸输入
此扫描结合使用 REGEX 掩码和输入掩码来创建一个强大的、仅限英尺和英寸的文本框。
ex = "[\d]+(?:\.[\d]+|)(?:\s\d+\/\d+|)(?:\s|\'|\\"|)[\d]+(?:\.[\d]+|)(?:\s\d+\/\d+|)(?:\'|\\"|)";
$('#feet').inputmask('Regex', { regex: ex });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/RobinHerbots/jquery.inputmask/3.x/dist/jquery.inputmask.bundle.js"></script>
So the following values are allowed:<br /><br />
5 6 (feet and inches separated by spaces)<br />
5'6" (feet and inches in the correct format)<br />
5.2 6 (decimal feet separated by spaces)<br />
5.2'6" (decimal feet in the correct format)<br />
5 6.1 (decimal inches separated by spaces)<br />
5'6.1" (decimal inches in the correct format)<br />
5.2 6.1 (decimal feet and inches separated by spaces)<br />
5.2'6.1" (decimal feet and inches in the correct format)<br />
5 6 1/2 (any combination above followed by a space and fraction)<br />
5.2'6.1 1/2" (again with decimals)<br />
78" (only inches)<br />
78.4" (only inches with a decimal)<br />
<input id="feet" />
<br />