Skip to content

Latest commit

 

History

History
449 lines (292 loc) · 6.65 KB

deck.mdx

File metadata and controls

449 lines (292 loc) · 6.65 KB

export { hack as theme } from 'mdx-deck/themes' import { Appear, Notes } from 'mdx-deck' import { Box } from 'grid-styled'

import { Invert, Split } from 'mdx-deck/layouts'

export { components } from "mdx-deck-code-surfer"

/hel{2}o\\sworld/i



DISCLAIMER: only javascript


regex => string/text

math => number


match

validation

replace


"say hello world".exec(/hello\\s(world)/) => ["hello world", "world"]
----
* > exec
1[0] > text/string
1[4] > regexp
1[1:3, 5] > exec/match
1[8:14] > result

/hello\\sworld/.test("say hello world") => true

/hello\\sworld/.test("say hello fulano") => false
----
* > validation
1[0] > regex
1[4] > value
1[1:3, 5] > test
1 > true
3 > false

"say hello world".replace(/world/, "fulano") => "say hello fulano"
----
* > example
1[0] > value
1[4] > regexp - value to replace
1[7] > new value
1[12] > result

other disclaimer: about "validation/test"

structural !== substantial

const cpf = /[0-9]{3}\.[0-9]{3}\.[0-9]{3}-[0-9]{2}/

cpf.test("443.443.443-23") => true
cpf.test("4432.4423.443-23") => false
----
* > sxample 
1 > regexp
3:4 > structural

Terminology

/[a-z0-9]+/gmi


/<pattern>/<modifiers>

new RegExp("<pattern>", "<modifiers>")
----
* > terminology
1[0,4] > delimeter
1[1:3] > pattern
1[5:7] > modifiers
3 > the same with class

Modifiers: g m i


"hello world".replace(/o/, "x") => "hellx world"
"hello world".replace(/o/g, "x") => "hellx wxrld"
----
* > example
1 > example without modifier g
1[4] > regexp
1[7] > new value
1[12] > result
2 > example with modifier g
2[5] > regexp
2[13] > result

const value = "hello fulano\\nhello deltrano\\nhello foo\\nhello bar"

value.replace(/^(hello)\s([a-z]*)$/gm, "$1 wooow")

// hello wooow\\nhello wooow\\nhello wooow\\nhello wooow
----
* > example
1 > value
3 > replace
3[4] > regexp
3[7] > new value
5 > result

/hello\\sworld/.test("Hello World") => false
/hello\\sworld/i.test("Hello World") => true
----
* > example
1[0] > regex without modifier i
1[4] > value
1 > result
2[1:4] > regex with modifier i
2 > result

Plus Modifiers: y u


Tips: áâãéêíîóôõúûç

npm install diacritics
require('diacritics').remove("Iлtèrnåtïonɑlíƶatï߀ԉ")
// "Internationalizati0n"

Pattern

\\0[a-z][A-Z][^a-z][0-9].\\D\\w\\\.\?(...)+.?(?=!<=<!...)?*+\b\B


  • Anchor
  • Meta-sequences
  • Quantifiers
  • Group
  • Characters

Pattern - Anchor

^ $ \\b \\B


P - Anchor ˆ $

/^a/ => aaaaaaa

/a$/ => aaaaaaa


Anchor \\b \\B

/a\\b/ = aaaa -- aaaaa -- aaaa

/a\\b/g = aaaa -- aaaaa -- aaaa

/a\\B/g = aaaa -- aaaaa -- aaaa


Pattern - Meta sequences

\\s \\S \\w \\W \\d \\D


Meta sequence \\s \\S

/\\s/g = aaaaa_aaaaaa_aaaaaaa_aaa

/\\S/g = aaaaa aaaaaa aaaaaaa aaa


Meta sequence \\w \\W

/\\w/g = aaaa12345aaaa123123

/\\W/g = aaaa12345aaaa123123


Meta sequence \\d \\D

/\\d/g = aaaa12345aaaa123123

/\\D/g = aaaa12345aaaa123123


Pattern - Quantifiers

a* a+ a{3} a{3,} a{3, 6}


Quantifiers a* a+ a?

/ba*/g = aaaaa3ba3aaaaaa3baaaaaa3b

/ba+/g = aaaaa3ba3aaaaaa3baaaaaa3b

/ba?/g = aaaaa3ba3aaaaaa3baaaaaa3b


Quantifiers a{3} a{3,}

/a{3}/g = a aa aaa aaaaa

/a{3,}/g = a aa aaa aaaaa

/a{3,4}/g = a aa aaa aaaaa


Pattern - Characters

[a] [a-z] [^a-z] [a-zA-Z] [0-9]


Characters [a] [!] [@]

/b[aei]/g = ba baa be bee bi bo bu

/b[aei]+/g = ba baa be bee bi bo bu

/bah[?!]/g = bah bah? bah! bah


Characters [^a] [^!] [^@]

/b[^aei]/g = ba baa be bee bi boo buu

/b[^aei]+/g = ba baa be bee bi boo buu

/bah[^?!]/g = bah bah? bah! bah


Characters [a-z] [A-Z] [0-9]

/h[a-z]* w[a-z]*/g = hello world 1000

/h[a-z]* w[a-z]* [0-9]*/g = hello world 1000

/h[^a-z]* w[^a-z]* [0-9]*/g = hi world 100 h123 w123 10


Pattern - Group

(a|b) (?:) (?=) (?!) (?<=) (?<!)


short intro: about group


/hello world/.test('hello world') => true
/hello (world)/.test('hello world') => true

"hello world".exec(/hello world/) => ["hello world"]
"hello world".exec(/hello (world)/) => ["hello world", "world"]

"hello world".replace(/hello world/, "hi world") => "hi world"
"hello world".replace(/hello (\w*)/, "hi $1") => "hi world"
"hello bar".replace(/hello (\w*)/, "hi $1") => "hi bar"
----
* > example
1:2 > test
4 > without group
5 > with group
5[10:15] > result
7 > without group
8:9 > with group
8[8] > replace with group
8[1],9[1] > dynamic text
8[13], 9[13] > dynamic result

Group (a|b)

/hi (foo|bar)/g = hi boo hi foo hi bar


Group (?=) (?!)

/foo (?=bar)/g = foo bar foo bz foo bar

/foo (?!bar)/g = foo bar foo bz foo bar


Group (?<=) (?<!)

/(?<=foo) bar/g = foo bar for bar foo bar

/(?<!foo) bar/g = foo bar for bar foo bar

is not yet supported on all browsers

Plus group: (?<name>)

/hello (?<planet>\\w*/g


BOOOM

/^(([^<>()[]\\.,;:s@"]+(.[^<>()[]\\.,;:s@"]+)*)|( ".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA -Z-0-9]+.)+[a-zA-Z]{2,}))$/


XRegExp

  • npm install xregexp
  • weekly downloads: 3.186.118

  • const date = /([0-9]{4}-[0-9]{2}-[0-9]{2})/
    
    const date = new RegExp('([0-9]{4}-[0-9]{2}-[0-9]{2})')
    
    const date = XRegExp(
        `(?<year>  [0-9]{4} ) -  # year
         (?<month> [0-9]{2} ) -  # month
         (?<day>   [0-9]{2} )    # day`, 'x');
    ----
    * > example
    1 > regexp native
    3 > regexp class
    5:8 > xregexp

    Where to apply

  • search
  • reformat content/fix content
  • find file
  • find content
  • validation
  • terminal
  • extrating data
  • cryptography

  • Tools

    https://regex101.com/