开发者

Javascript: s/digit/" " x that digit/g

开发者 https://www.devze.com 2023-01-01 21:57 出处:网络
I 开发者_StackOverflow社区would like to match a digit and replace it with that many spaces. Can this be done in less than 9 lines of code?

I 开发者_StackOverflow社区would like to match a digit and replace it with that many spaces. Can this be done in less than 9 lines of code?

Example

"asnthsnth4nts4h3n" --> "asnthsnth    nts    h   n"


Try this:

"asnthsnth4nts4h3n".replace(/\d+/g, function($0) { return Array(parseInt($0, 10)+1).join(" "); })
0

精彩评论

暂无评论...
验证码 换一张
取 消