Skip to content

Commit 6c7f870

Browse files
committed
about regexs
#99
1 parent c11c819 commit 6c7f870

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

js/red-javascript-style-guide/readme.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,31 @@ const max = Math.max(...numbers);
365365
```
366366

367367

368+
### regular expressions
369+
370+
Avoid when possible. Regular expression is a powerfull language that overlaps with JS constructs. Often a single line can obscure recursive and complex instructions. The tooling to debug and assess perfomance of regexs are lower than JS.
371+
372+
Avoid the constructor syntax. Use named groups.
373+
374+
```js
375+
let r = /[a-z]+/;
376+
```
377+
378+
#### Alternatives
379+
380+
##### Use regular objects
381+
382+
Regex are sometimes used to extract informations out of a big string. Consider using an object instead that can be extracted with JSON for example.
383+
384+
##### Raw String
385+
386+
* `String.prototype.includes`
387+
* `String.prototype.indexOf`
388+
* `String.prototype.replace`
389+
* `String.prototype.endsWith`
390+
* `String.prototype.startsWith`
391+
392+
368393
### ternary operator
369394

370395
Prefer `if else`, as they can be extended and are cleaner.

0 commit comments

Comments
 (0)