Replace all left spaces with a specific string

前端 未结 5 516
时光说笑
时光说笑 2020-12-19 11:18

I have following code to replace left spaces with a specific string but, It is not working as I want.

5条回答
  •  既然无缘
    2020-12-19 12:09

    You can use a callback function to generate the same length string which contains x.

    console.log('  asdadasdad as asdasd asasd'.replace(/^\s+/, m => new Array(m.length).fill('x').join('')))


    Or alternately with positive look-behind(not supported widely, check here).

        console.log('  asdadasdad as asdasd asasd'.replace(/(?<=^\s*)\s/g, 'x'))

提交回复
热议问题