Replace all left spaces with a specific string
问题 I have following code to replace left spaces with a specific string but, It is not working as I want. console.log(' asdadasdad as asdasd asasd'.replace(/^\s+/, 'x')) It changed all left spaces with x, but It should change every left spaces with one x. But I just need this output: xxasdadasdad as asdasd asasd How can I do it ? Thanks so much. 回答1: This will surely do in two lines: var str =' asdadasdad as asdasd asasd'; console.log(str.trim().padStart(str.length, 'x')); 回答2: You may use a