Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substring set of string between given characters in Javascript

String I get is

["2021-01-13T09:45:48.046Z","2021-01-14T09:45:48.096Z","2021-01-15T09:45:48.099Z"]

Here I want to delete substring from T to Z for converting to this form:

["2021-01-13","2021-01-14","2021-01-15"]

I did like that but it replaces only the first string and deletes others, How can I do this separately for each

notFound=notFound.replace(/\T.*\Z/g, '');

Can I do this with the javascript substring function?

like image 643
Neo Avatar asked Dec 17 '22 12:12

Neo


2 Answers

You could replace with an nongready search.

const
    data = '["2021-01-13T09:45:48.046Z","2021-01-14T09:45:48.096Z","2021-01-15T09:45:48.099Z"]',
    result = data.replace(/T.*?Z/g, '');

console.log(result);
like image 84
Nina Scholz Avatar answered May 04 '23 10:05

Nina Scholz


Because you said String so you will have to parse it.

const str = '["2021-01-13T09:45:48.046Z","2021-01-14T09:45:48.096Z","2021-01-15T09:45:48.099Z"]';

let x = JSON.parse(str).map((e) => e.replace(/\T.*\Z/g,  ''));

console.log(x);
like image 25
sourav satyam Avatar answered May 04 '23 08:05

sourav satyam