3 回答
TA贡献1842条经验 获得超22个赞
您可以利用正则表达式来测试字符串并提取 4 位数字:
var fileName = "Ext 01_main Concept A0000"
console.log(fileName.match(/\d{4}/)); // result: [ 0000 ]
如果有问题的 4 位数字始终出现在字符串的末尾,您还可以使用$令牌来确保有问题的 4 位数字显示如下:
var fileName = "Ext 01_main Concept A0000"
console.log(fileName.match(/\d{4}$/)); // result: [ 0000 ]
TA贡献1943条经验 获得超7个赞
你可以使用正则表达式来做到这一点:
var fileName = "Ext 01_main Concept A0000";
// The regexp will search for any 4 digits from 0000 to 9999
var match = fileName.match(/[0-9]{4}/g);
// The variable match will have an array with all matching values
// In this case the log should write [ '0000' ]
console.log(match);
TA贡献1815条经验 获得超10个赞
您可以对字符串中的数字进行正则表达式并测试:
var fileName = "Ext 01_main Concept A0000"
const regex = /\d{4}/; // if it's always the last, it could be /\d{4}$/
thenum = parseInt(fileName.match(regex)[0],10); // "0000"
console.log( thenum > 0 && thenum < 9999 );
您的代码可能会工作,但最坏的情况是它会执行 9998 次不必要的检查!
添加回答
举报
