Add expandTabs

This commit is contained in:
Angelos Chalaris
2020-06-01 17:05:39 +03:00
parent 125fda22f3
commit 60d2238718

16
snippets/expandTabs.md Normal file
View File

@ -0,0 +1,16 @@
---
title: expandTabs
tags: string,regexp,beginner
---
Convert tabs to spaces, where each tab corresponds to `count` spaces.
Use `String.prototype.replace()` with a regular expression and `String.prototype.repeat()` to replace each tab character with `count` spaces.
```js
const expandTabs = (str, count) => str.replace(/\t/g, ' '.repeat(count));
```
```js
expandTabs('\t\tlorem', 3); // ' lorem'
```