简体   繁体   English

如何使用正则表达式从 javascript 中的 HTML 字符串中删除所有时间戳?

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

I have some HTML code stored in a variable:我有一些 HTML 代码存储在变量中:

var htmlString = ...

I want to remove all timestamps that are in this string, of the form (example): 2021-04-26 04:15:04.309 .我想删除此字符串中的所有时间戳,格式为(示例): 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用正则表达式从 JavaScript 中的字符串中删除所有标点符号? - How can I strip all punctuation from a string in JavaScript using regex? 使用 RegEx 从字符串中获取所有 (H)H:MM 时间戳 - Get all (H)H:MM timestamps from a string using RegEx 使用正则表达式javascript从字符串中删除HTML标签和空格 - Remove HTML Tags and whitespace from string using regex javascript Javascript函数使用正则表达式删除字符串中的所有数字 - Javascript function to remove all digits from a string using regex 如何从html字符串中删除所有JavaScript(使用javascript或jquery)? - How to remove all javascript from an html string (with javascript or jquery)? 如何使用Java中的RegExp从字符串中删除插入符号(^)? - How can I remove the caret(^) from a string using a RegExp in Javascript? 如何使用 JavaScript 从字符串中删除字符? - How can I remove a character from a string using JavaScript? 给定一个字符串,如何使用 JavaScript 删除除特定“标签”(及其“子项”)之外的所有“HTML 标签”? - Given a string, how can I use JavaScript to remove all 'HTML tags' except a specific 'tag' (and its 'children')? 如何在不使用Javascript中的正则表达式的情况下删除字符串括号中的文本? - How can I remove text in brackets in a string without using regex in Javascript? 如何使用JavaScript Regex从字符串中删除剩余的逗号? - How to remove remaining commas from a string using JavaScript Regex?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM