Skip to content

Latest commit

 

History

History
27 lines (17 loc) · 822 Bytes

expandTabs.md

File metadata and controls

27 lines (17 loc) · 822 Bytes
标题 标签
expandTabs(将制表符转换为空格) string,regexp(字符串,正则表达式)

将制表符转换为空格,其中每个制表符对应于计数空格。

  • 使用带有正则表达式的 String.prototype.replace()String.prototype.repeat() 将每个制表符替换为计数空格。

代码如下:

const expandTabs = (str, count) => str.replace(/\t/g, ' '.repeat(count));

调用方式:

expandTabs('\t\tlorem', 3); // '      lorem'

应用场景

结果如下:

<iframe src="codes/javascript/html/expandTabs.html"></iframe>