How to use regex to check if a javascript string contains a pattern

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ย. 2022
  • Regex is very useful, be sure to take time to learn it.
    My VSCode Extensions:
    - theme: material community high contrast
    - fonts: Menlo, Monaco, 'Courier New', monospace
    - errors: Error Lens
    - extra git help: Git Lens
    - tailwind css intellisense
    - indent rainbow
    - material icon theme
    - prettier & eslint
    - ES7+ React Snippets
    ------------
    🤑 Patreon / webdevjunkie
    🔔 Newsletter eepurl.com/hnderP
    💬 Discord / discord
    📁. GitHub github.com/codyseibert/youtube

ความคิดเห็น • 56

  • @VikashXman
    @VikashXman ปีที่แล้ว +59

    More regex please

  • @alpachino468
    @alpachino468 ปีที่แล้ว +14

    Regular Expressions is the only thing that can make me drop to the floor crying uncontrollably with my thumb in my mouth 😢

    • @NickCombs
      @NickCombs ปีที่แล้ว +3

      If a pattern is getting long and unwieldy, I recommend breaking it up into multiple tests. Just be sure to name each test descriptively so you don't forget what they each do.

    • @blocktube1449
      @blocktube1449 ปีที่แล้ว

      😂

  • @zir1010011010
    @zir1010011010 ปีที่แล้ว +12

    The g is not needed when simply testing for presence, creates a problem if you were to run the re.test again:
    const text1 = "something dog5 is great";
    const text2 = "cat1 foo";
    const re = /[a-z]+\d/g
    console.log(re.test(text1)); // true
    console.log(re.test(text2)); // false
    re.test(text2) should return true, but the global flag causes regex to assume that you want to check for next matches (at later index than dog5) instead of starting from the beginning

  • @armandopenatamayo691
    @armandopenatamayo691 ปีที่แล้ว +8

    You can do some crazy stuff with your regex. Nice video

  • @YamiSuzume
    @YamiSuzume ปีที่แล้ว +9

    There it is again. "We use a ReGular Expression, also called ReJex"

    • @inactiveaccount4829
      @inactiveaccount4829 ปีที่แล้ว +1

      This is how most people pronounce it. Sure, it’s odd.

    • @NickCombs
      @NickCombs ปีที่แล้ว +2

      Probably because it looks similar the Regedit (registry editor).

    • @YamiSuzume
      @YamiSuzume ปีที่แล้ว

      @@NickCombs wat

    • @NickCombs
      @NickCombs ปีที่แล้ว +3

      @@YamiSuzume it's a windows utility that's been around forever

    • @YamiSuzume
      @YamiSuzume ปีที่แล้ว

      @@NickCombs No shit, I just think that feels a bit far-fetched

  • @andrewmonger5433
    @andrewmonger5433 ปีที่แล้ว +1

    love the shorts. great stuff

  • @user-fv4xc4qp2h
    @user-fv4xc4qp2h ปีที่แล้ว

    not a channel, but a treasure trove of useful information 🔥

  • @AntonioSantos-ve6zv
    @AntonioSantos-ve6zv ปีที่แล้ว

    First time I understand something on regex. Thanks

  • @SeibertSwirl
    @SeibertSwirl ปีที่แล้ว +1

    Good job babe!

  • @dante4957
    @dante4957 ปีที่แล้ว

    Keep up the good content

  • @LazyShady
    @LazyShady ปีที่แล้ว

    More this please.

  • @mercygotnerfed315
    @mercygotnerfed315 ปีที่แล้ว

    Regex so usefull more please

  • @willyhorizont8672
    @willyhorizont8672 ปีที่แล้ว +1

    Make full regex tutorial please

  • @legitjimmyjaylight8409
    @legitjimmyjaylight8409 ปีที่แล้ว +2

    Im pretty good with regexes, except for look arounds, I tend to have to always have to look up how those work again, and I think named groups is a thing, I would have to check back how they work, and some of the shortforms for any whitespace, etc.
    But I am good with the . ? + * ^ […] {n,m} {n,} and {,n} symbols and the meaning of an odd vs even chain of \ and know what the /.../g g means and I think there is also /.../i i for ignorecase, and I know what greediness means.

    • @NickCombs
      @NickCombs ปีที่แล้ว

      Nice, I pretty much always dev patterns on regex101 so I have the cheat sheet and breakdown right there. You can even save and share results.

  • @kimbapslayer1995
    @kimbapslayer1995 ปีที่แล้ว

    Man you broke down Regex the easiest I’ve ever seen lol. Thx

  • @giyuu4603
    @giyuu4603 ปีที่แล้ว

    Nice ! And the red text behind each line is extension or something ?

  • @TheFootballPlaya
    @TheFootballPlaya ปีที่แล้ว

    Can you do a javascript to python comparison? like the things you can do in javascript that you can similarly do in python?

  • @ishanmaduranga6459
    @ishanmaduranga6459 ปีที่แล้ว

    What are the extensions you use in VSCode?

  • @codingwithkrpajay
    @codingwithkrpajay ปีที่แล้ว

    🙌🙌🙌

  • @patriotir
    @patriotir ปีที่แล้ว +1

    Can you make a tutorial for RegEx please. Its so confusing

    • @WebDevCody
      @WebDevCody  ปีที่แล้ว +1

      Yeah I could try to make one

    • @patriotir
      @patriotir ปีที่แล้ว

      @@WebDevCody Thank you so much! you explain everything so simple which is easy to understand and thanks for all your great videos!

    • @dizzyrocket2000
      @dizzyrocket2000 ปีที่แล้ว +1

      DON'T WASTE YOUR TIME learning regex. There are websites that will write the regex patterns for you; you just tell it what you want the regex to check for and it gives you the code instantly

  • @melih.a
    @melih.a ปีที่แล้ว

    What’s the point of the re.test(text) and not directly re.test why wouldn’t it work?

  • @user-db9ri4qw8e
    @user-db9ri4qw8e ปีที่แล้ว

    Hello i am very cool!

  • @aamiramin6112
    @aamiramin6112 ปีที่แล้ว +2

    Amazing! just need to know that [a-z]+ means more than one character?

    • @DashNothing
      @DashNothing ปีที่แล้ว

      The + means anything before it will appear at least once or more times

    • @legitjimmyjaylight8409
      @legitjimmyjaylight8409 ปีที่แล้ว

      The [a-z] means any lowercase letter, the + means it can appear 1 or more times in a row.

    • @jacksonbourne
      @jacksonbourne ปีที่แล้ว +1

      + is shorthand for {1,} and * is shorthand for {0,}

    • @stevenstraker5105
      @stevenstraker5105 ปีที่แล้ว +1

      The "a-z" is range that is shorthand for those letters and everything between, so you can do [a-e] if you only want to match on the letters "a,b,c,d,e". If you specially want certain characters you can just include those: [rstlne] for the Wheel of Fortune letters :D

  • @dizzyrocket2000
    @dizzyrocket2000 ปีที่แล้ว

    Guys, DON'T WASTE YOUR TIME learning regex expressions; they are immensely difficult to learn. There are websites that will write regex patterns for you, you just describe what you want the regex to do and it will give you the code instantly.

    • @haraldbackfisch1981
      @haraldbackfisch1981 4 หลายเดือนก่อน

      By describing a pattern you have effectively already made a regex. If u just don't know the symbols then may the hallucinating mind of gippidy help u.. I wouldn't trust on it... regex are already considered very fragile in prod, if u don't get them as the one putting them in then it's a disaster waiting to happen.

  • @RolandGiersig
    @RolandGiersig ปีที่แล้ว

    Of course now this regexp works only for english text, as other languages use chars outside the ASCII range a-z... 🤷‍♂️

    • @NickCombs
      @NickCombs ปีที่แล้ว

      In Javascript, we have to use the \u#### token to match specific unicode chars. Other coding languages include \X which match any unicode char, but with the caveat that it also matches end of line.

  • @zabiulahmed2174
    @zabiulahmed2174 ปีที่แล้ว

    Also known as a "rejex" .? I thought it was a regex

  • @ahmedmohammed-wi8qp
    @ahmedmohammed-wi8qp ปีที่แล้ว

    how is the output show next to code while he write the code show him their error or what ever

  • @dkorjey
    @dkorjey ปีที่แล้ว

    So, I'm (beginner?) in a programming. But why everyone uses constants everywhere?!

    • @reshusanan5499
      @reshusanan5499 ปีที่แล้ว +1

      Well ECMA6

    • @DashNothing
      @DashNothing ปีที่แล้ว

      It"s a good principle to declare variables you know aren"t going to change as const

    • @69k_gold
      @69k_gold ปีที่แล้ว +1

      Actually, consts are used this often only in javascript, it's actually a good practice.
      Before that, you need to learn about global and local declaration of variables and how var, let and const are used there. If you're learning JS rn, I recommend reading ES6 or 2015 documentation of JS

    • @dkorjey
      @dkorjey ปีที่แล้ว

      @@69k_gold ok, thanks! I got it!

    • @NonexistentCZ
      @NonexistentCZ ปีที่แล้ว

      @@69k_gold they're not only used that much in JS, low level programming languages like C++ and Rust use them a lot as well, however it's usually for different reasons, in JS, const is a guarantee that an immutable object will remain untouched, which mostly just supports the functional paradigm that JS likes and does really well, however in lower level programming it's just a promise to the compiler that we won't change the value, for instance we can guarantee that way that a function will be pure by passing a constant reference or pointer, however we're offered much more freedom, as we can always cast the const away and change the value, at the cost of hard to debug code with potential undefined behavior if you don't handle things properly.