Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Sunny-117 opened this issue Nov 3, 2022 · 3 comments
Open

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

Sunny-117 opened this issue Nov 3, 2022 · 3 comments

Comments

@Sunny-117
Copy link
Owner

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));
@Leadgq
Copy link

Leadgq commented Jan 12, 2023

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))

@kangkang123269
Copy link

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

@veneno-o
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants