版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26924703/article/details/81458210
// 以下的静态方法实现了:把串s中第一个出现的数字的值返回。
// 如果找不到数字,返回-1
// 例如:
// s = "abc24us43" 则返回2
// s = "82445adb5" 则返回8
// s = "ab" 则返回-1
function firstNum(str) {
let reg = /\d/;
if (reg.test(str)) {
let index = str.search(reg);
return str.charAt(str.search(reg));
} else {
return -1;
}
}
console.log(firstNum("ab"));