I have following code to replace left spaces with a specific string but, It is not working as I want.
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'))