列表

详情


FED79. 判断是否符合指定格式

描述

给定字符串 str,检查其是否符合如下格式
1、XXX-XXX-XXXX
2、其中 X 为 Number 类型

示例1

输入:

'800-555-1212'

输出:

true

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 869ms, 内存消耗: 77876KB, 提交时间: 2020-12-25

{"css":"","js":"function matchesPattern(str) {\n    return (/^\\d\\d\\d\\-\\d\\d\\d\\-\\d\\d\\d\\d$/.test(str));\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 872ms, 内存消耗: 77876KB, 提交时间: 2020-12-24

{"css":"","js":"function matchesPattern(str) {\nreturn/^(\\d{3}-){2}\\d{4}$/.test(str);\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 874ms, 内存消耗: 77872KB, 提交时间: 2020-11-18

{"css":"","js":"function matchesPattern(str) {\nvar reg=/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/;\n    return reg.test(str);\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 878ms, 内存消耗: 77864KB, 提交时间: 2020-12-21

{"css":"","js":"function matchesPattern(str) {\n    let arr = str.split('-');\n    if(arr.length == 3){\n        let isNumber =  arr.every(function(value){\n            return value == parseInt(value);\n        });\n        if(isNumber && arr[0].length == 3 && arr[1].length == 3 && arr[2].length == 4){\n            return true;\n        }\n    }\n    return false;\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 881ms, 内存消耗: 77900KB, 提交时间: 2020-12-28

{"css":"","js":"function matchesPattern(str) {\n    return /^\\d{3}-\\d{3}-\\d{4}$/.test(str)\n}","html":"","libs":[]}

上一题