DEV Community

loading...

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

Collapse
johkycheng profile image
Johky • Edited

If I have to do this, I will use only regex replace instead of using both split and regex, where regex only already can achieve this :

var str = '"2019-08-02T00:00:00.000Z"';

var res = str.replace(/.*(\d{4}\-\d{2}\-\d{2}).*/, "$1");

This way also will not care about other string inside the string, it will just try to find 9999-99-99 and remove others.
It will accept any format with this pattern.

Eg. :
2019-08-02 00:00:00
Friday, 2019-08-02 00:00:00