Nest all content into snippets
This commit is contained in:
21
snippets/js/s/expand-tabs.md
Normal file
21
snippets/js/s/expand-tabs.md
Normal file
@ -0,0 +1,21 @@
|
||||
---
|
||||
title: Expand tabs into spaces
|
||||
type: snippet
|
||||
language: javascript
|
||||
tags: [string,regexp]
|
||||
author: chalarangelo
|
||||
cover: houses-rock-sea
|
||||
dateModified: 2020-09-15T16:28:04+03:00
|
||||
---
|
||||
|
||||
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'
|
||||
```
|
||||
Reference in New Issue
Block a user