我想创建一个正则表达式来检查有效维度 JavaScript 中的长度 x 宽度 x 高度。
例如90.49 x 34.93 x 40.64
我打算使用的示例代码:
<小时/>var dimensionRegex = 'the regular expression for validation dimension string';
var tempDimension = 'dimension string input by the user';
if (dimensionRegex.test(tempDimension)) {
return true;
} else {
return false;
}
任何帮助将不胜感激。
最佳答案
为什么要使用正则表达式呢?您可以split('x')
字符串,然后从数组元素中去除前导/尾随空格。然后你parseInt(elem, 10)
数组元素就得到了你需要的东西。
但是,如果您确实想验证格式:
var dimValid = /^\d+(\.\d+)? x \d+(\.\d+)? x \d+(\.\d+)?$/.test(tempDimension);
这将匹配数字 x 数字 x 数字
。
关于javascript 正则表达式 维度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5063217/