Files
30-seconds-of-code/snippets/expandTabs.md
2020-09-15 21:52:00 +03:00

17 lines
412 B
Markdown

---
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'
```