维度的javascript正则表达式

Ami*_*abh 0 javascript regex

我想创建一个正则表达式来检查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;   
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.

Thi*_*ter 5

为什么要使用正则表达式?您可以只是split('x')字符串,然后从数组元素中删除前导/尾随空格.然后你parseInt(elem, 10)的数组元素,你有你需要的.

但是,如果您确实要验证格式:

var dimValid = /^\d+(\.\d+)? x \d+(\.\d+)? x \d+(\.\d+)?$/.test(tempDimension);
Run Code Online (Sandbox Code Playgroud)

这将匹配number x number x number.