DEV Community

loading...

Discussion on: Two string methods every JavaScript developer should know.

Collapse
jalovatt profile image
Adam Lovatt

So you thought of using a RegEx, ended up discovering Split, then had to use a RegEx in your Replace anyway?

At that point you might as well go all-in:

const justDate = date.match(/"(.+)T/)[1]

(Typed on a phone, may not be perfect)

Collapse
itz_giddy profile image
Audiophile Author

Hi Adam, so I guess the point for me was applying something that I understood. My knowledge of RegEx isn't the best, and I wanted to apply something in the code base I could explain if asked. Thanks for sharing!

Collapse
jalovatt profile image
Adam Lovatt • Edited

That's fair, though I would argue the level of RegExiness involved here is something everyone should make a point of learning because it solves so many problems.

An even shorter alternative, since you know the input format won't change: const justDate = date.slice(1, 11).

Collapse
euler2718 profile image
John Corley

Yea I was thinking the exact same thing. Probably didn't notice the /"/g was regex