Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

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.*/g, '');

Can I do this with the javascript substring function?

question from:https://stackoverflow.com/questions/65626912/substring-set-of-string-between-given-characters-in-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.*/g,  ''));

console.log(x);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...