regex quick guide
parent
86c42be861
commit
d08d1ac1ba
@ -0,0 +1,52 @@
|
|||||||
|
# REGEX Quick Guide
|
||||||
|
|
||||||
|
* [Regex 101](https://regex101.com/) : Have sample code generator
|
||||||
|
|
||||||
|
* `Atom` and `Notepad++` support Regex `Find` / `Replace`. Can use to test regex expression
|
||||||
|
|
||||||
|
* Javascript : Enclosed within `/.../`
|
||||||
|
|
||||||
|
* Control characters
|
||||||
|
|
||||||
|
* `.` : Any Character Except New Line
|
||||||
|
* `\d` : Digit (0-9)
|
||||||
|
* `\D` : Not a Digit
|
||||||
|
* `\w` : Word Character (a-z, A-Z, 0-9, _)
|
||||||
|
* `\W` : Not Word Character
|
||||||
|
* `\s` : Whitespace (space, tab, newline)
|
||||||
|
* `\S` : Not Whitespace
|
||||||
|
* `\b` : Word Boundary
|
||||||
|
* `\B` : Not Word Boundary
|
||||||
|
* `^` : Beginning of a String
|
||||||
|
* `$` : End of a String
|
||||||
|
|
||||||
|
* Other
|
||||||
|
|
||||||
|
* `[]` : Matches Characters in brackets
|
||||||
|
* `[^ ]` : Matches Characters NOT in brackets
|
||||||
|
* `|` : Either Or
|
||||||
|
* `( )` : Group
|
||||||
|
|
||||||
|
* Quantifiers
|
||||||
|
|
||||||
|
* `*` : 0 or more
|
||||||
|
* `+` : 1 or more
|
||||||
|
* `?` : 0 or One
|
||||||
|
* `{3}` : Exact Number
|
||||||
|
* `{3,4}` : Range of Numbers (Minimum, Maximun)
|
||||||
|
|
||||||
|
* MetaCharacters (Need to be escaped)
|
||||||
|
|
||||||
|
* `.[{()\^$|?*+` : Need to be escaped with `\`.
|
||||||
|
* Eg String = `\*`
|
||||||
|
* To form the character list, escape it as shown `\\\*`
|
||||||
|
|
||||||
|
* Useful regex
|
||||||
|
|
||||||
|
| regex | Description | Sample javascript |
|
||||||
|
| -------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
|
||||||
|
| /\d(?=\d{4})/g | mask digits and keep remaining 4 digits | '12345678'.replace(/\d(?=\d{4})/g, "*") |
|
||||||
|
| /(\d+)(\d{4})/ | mask digit. Group into group1 (length - 4 digits) and group2 (4-digit) | '12345678'.replace(/(\d+)(\d{4})/, (match, g1, g2) => { return g1.replace(/\d+/, '*'.repeat(g1.length) + g2) }); |
|
||||||
|
| | | |
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue