IE11 Intl.DateTimeFormat 输出与正则表达式不匹配

IE11 Intl.DateTimeFormat output won't match regex

这既是一个问题也是一个答案,以便下次我遇到此错误和Google答案时,我会在这里找到它。

的输出
Intl.DateTimeFormat('en-US', {
  year: 'numeric',
  month: 'numeric',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second: 'numeric',    
})

类似于 IE11 中的 3/7/2020 6:43:00 AM。它看起来像这个正则表达式(请不要评判我的正则表达式技能):

/:[0-9]{2}:[0-9]{2}/g

应该匹配 :43:00 部分,在其他浏览器中也是如此。但它不在 IE11 中。给出了什么?

IE11 的输出包含不可见的从左到右的标记 (\u200E),导致正则表达式不匹配。

我的解决方案是先删除这些,然后再尝试对字符串进行任何操作:

myDateTimeStr.replace(/\u200E/g, '')

我从这个issue report on react-intl中得到了线索。