You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: js/red-javascript-style-guide/readme.md
+25Lines changed: 25 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -365,6 +365,31 @@ const max = Math.max(...numbers);
365
365
```
366
366
367
367
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
+
368
393
### ternary operator
369
394
370
395
Prefer `if else`, as they can be extended and are cleaner.
0 commit comments