简体   繁体   中英

How can I remove all timestamps from HTML string in javascript, using regex?

I have some HTML code stored in a variable:

var htmlString = ...

I want to remove all timestamps that are in this string, of the form (example): 2021-04-26 04:15:04.309 . Something like:

var htmlStringWithoutTimestamps = removeTimestamps(htmlString)

Hoy can I do this? Maybe with regex?

I've adapted code from another answer . The logic is pretty understandable.

 function removeTimestamps(htmlString) { var reg = /[0-9]{1,4}-[0-9]{1,2}-[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}\.[0-9]{1,3}/g return htmlString.replace(reg, ""); } var htmlString = 'hello 2021-04-26 04:15:04.309: or:AB2021-11-11 11:11.11.222CD' var htmlStringWithoutTimestamps = removeTimestamps(htmlString) console.log(htmlStringWithoutTimestamps)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM