How to pad out line to certain length using regex replace and snippets

前端 未结 2 1881
孤街浪徒
孤街浪徒 2020-12-22 13:07

// My Section -----------------------------------------------------------------

The above is the desired result.


Imagine this scenario

2条回答
  •  一向
    一向 (楼主)
    2020-12-22 13:28

    You can use callback function of replace and based on length you can replace values

    let str = `// ----------------------------------------------------------------------------
    // firebase
    
    // ----------------------------------------------------------------------------
    // utils`
    
    let final = str.replace(/^.*$/gm, (match)=>{
      return match.length === 0 ? match : match.length > 20 ? match.substr(0,20) : match + `-`.repeat(20-match.length)
    })
    
    console.log(final)

提交回复
热议问题