chenhuiYj / chenhuiYj/note

练习题1:找出字符串包含的所有连续的数字

Open
#12 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
6
Forks
0
PR merge metrics
No merged PRs in 30d

Description

编写一个函数 handle ,函数接受一个字符串,找出该字符串里面所由N(N>1)位连续数字组成的数值,

handle('asd123456789asdwa1234sdw578q6e')
// result:   ['123456789','1234']

方法一

function handle(str){
    let _strNumber='123456789'
    let arr=str.replace(/\D+/g,' ').split(/\s+/)
    return arr.filter(item=>item.length>0&&_strNumber.includes(item))
}

//或者缩写成一行代码,但是不建议这样写,因为你链式调用太长,可读性和维护性变差
function handle(str){
    return str.replace(/\D+/g,' ').split(/\s+/).filter(item=>item.length>0&&'123456789'.includes(item))
}

方法二

function handle(str){
    return str.match(/\d+/g,' ').filter(item=>item.length>1&&'123456789'.includes(item))
}

然后,如果要对题目进行改一下,比如上面的字符串,有 578 这个片段,虽然整个不是连续的数字,但是 78 是连续的,如果要兼容这种情况呢?

function handle(str){
   return str.match(/(0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9))\d+/g)
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Use the issue's handle examples and expected arrays as the specification; no source file or test path is named. Review the existing exercise content and ensure it clearly covers both multi-digit runs and consecutive-digit subsequences, with outputs matching the examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
content, documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.