简体   繁体   English

如何删除所有隐藏字符(冒号,数字和“ AM”或“ PM”除外)

[英]How to remove all hidden characters except colons, numbers and 'AM' or 'PM'

I have a string that has the time in it like this 我有一个这样的字符串有时间

"6:00:00 AM" or "10:15:00 PM" “ 6:00:00 AM”或“ 10:15:00 PM”

I know the string has hidden characters in it so I would like to use a regex to replace all characters except colons, numbers and AM or PM. 我知道该字符串中包含隐藏的字符,因此我想使用正则表达式来替换除冒号,数字和AM或PM以外的所有字符。

not sure if this below works because I have a string comparison check that is still failing. 不知道这是否可行,因为我的字符串比较检查仍然失败。

selectedTime = selectedTime.replace(/^\w:\s/g, "");

I also tried 我也试过

selectedTime = selectedTime.replace(/[^\w:\s]/g, "");

This regex replaces all characters except digit, colon, and AM PM 此正则表达式替换除数字,冒号和AM PM以外的所有字符

selectedTime = selectedTime.replace(/[^\d:AMP]/gi, "");

Here is DEMO 这是演示

As stated... This replaced everything except numbers (the ^\\d part), colon (the ^: part) and AM/PM (the ^AMP part). 如前所述...它替换了除数字(^ \\ d部分),冒号(^:部分)和AM / PM(^ AMP部分)以外的所有内容。

selectedTime = selectedTime.replace(/[^\\d:AMP]/g,""); selectedTime = selectedTime.replace(/ [^ \\ d:AMP] / g,“”);

You might also test using https://regex101.com 您也可以使用https://regex101.com进行测试

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

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