假设我想知道一个字符串是否包含5个或更多连续的连续数字.
var a = "ac39270982"; // False
var a = "000223344998"; // False
var a = "512345jj7"; // True - it contains 12345
var a = "aa456780"; // True - it contains 45678
Run Code Online (Sandbox Code Playgroud)
是否有可用于实现此目的的RegEx?它是否也能够在以下情况下工作?
var a = "5111213141587"; // True
Run Code Online (Sandbox Code Playgroud)
这应该是真的,因为它包含11,12,13,14,15.
我不确定是否可以检查提供的示例(单位数,两位数字)以及更大的数字(三位数等).
function NstreamsOfNumberN (str) {
for (let i = 0; i < str.length; i++) {
let numBeingConsidered = Number(str[i]);
let numOfComparisonsToBeDone = numBeingConsidered - 1;
for (let j = i; j < numOfComparisonsToBeDone + i; j++) {
if (str[j] != str[j+1]) {break}//compare neigbourin nums
else if ((j - i + 1) === numOfComparisonsToBeDone)
{ let theNwithNstreams = numBeingConsidered
return [str, (theNwithNstreams), true]}
//(j - i + 1) equals num of comparisons that has been done.
}
}
return [str,null,false]
}
NstreamsOfNumberN('334775555583444582')
9 streams of the number 9
8 streams of the number 8
7 streams of the number 7 ...
3 streams of the number 3
2 streams of the number 2.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1132 次 |
| 最近记录: |