Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

判断字符串中是否存在连续的三个数

Sunny-117 opened this issue · comments

var str = "013d1we22ewfa33rr4rwq0dsf00dsf9fas999";

var getNum = function (Str, isFilter) {
    //用来判断是否把连续的0去掉
    isFilter = isFilter || false;
    const finallyResult = [];
    // 精髓在正则
    var arr = Str.match(isFilter ? /[1-9]\d{1,}/g : /\d{2,}/g);
    const result = arr.map(ele => ele);
    console.log(result); //连续数字的数组
    for (let i = 0; i < result.length; i++) {
        if (result[i].length === 3) {
            finallyResult.push(result[i]);
        }
    }
    return finallyResult; // 连续的三个数字
};
console.log(getNum(str));

const collectStrCount = (str) => {
let reg = /\d{2,}/g;
const resultList = [];
while (res = reg.exec(str)) {
reg.lastIndex++;
resultList.push(res[0])
}
return resultList.filter(item => item.length === 3);
}
console.log(collectStrCount(str2))

str.match(/(\d{3})/g) 啊哈?

const reg = /^.*(.)\1\1.*$/