【问题标题】:Use regular expression to capture dates in utf-8使用正则表达式捕获 utf-8 中的日期
【发布时间】:2019-07-21 07:39:55
【问题描述】:

我使用 gmail api 获取电子邮件内容,然后在 node.js 中将其转换为字符串。

Buffer.from(dataToDecode, 'base64').toString('utf8')

然后我使用正则表达式在文本中搜索日期。例如2019 年 2 月 27 日

/[A-Z][a-z]{2} [0-9]{2}, [0-9]{4}/g

它没有匹配,但是当我 console.log 内容时,日期是存在的。然后我将日期复制到一些在线解码工具,结果是

\xe2\x80\x8c\x46\xe2\x80\x8c\x65\xe2\x80\x8c\x62\xe2\x80\x8c\x20\xe2\x80\x8c\x32\xe2\x80\x8c\x37\xe2\x80\x8c\x2c\xe2\x80\x8c\x20\xe2\x80\x8c\x32\xe2\x80\x8c\x30\xe2\x80\x8c\x31\xe2\x80\x8c\x39\xe2\x80\x8c\x0a

\x46\x65\x62\x20\x32\x37\x2c\x20\x32\x30\x31\x39

两者都可以给出相同的“2019 年 2 月 27 日”。如何使用正则表达式捕获第一种编码(即较长的编码)?

【问题讨论】:

标签: node.js regex utf-8


【解决方案1】:

1。检查unicode table

2。设置条件:

UTF-8:正则表达式描述

\x20: [\s] 空格

\x2C: [\,] 逗号

\x30-\x39: [0-9] 位数字

\x41-\x5A: [A-Z] 大写字母

\x61-\x7A: [a-z] 小写字母

模式

字符串:Feb 27, 2019

正则表达式:/[A-Z][a-z][a-z]\s\d\d\,\s\d{4}/g

UTF-8:/[\x41-\x5A][\x61-\x7A]{2}\x20[\x30-\x39]+\x2C\x20[\x30-\x39]{4}/g

Regex101 demo

【讨论】:

  • 有没有人解决问题删除\xe2\x80\x8c ?
猜你喜欢
  • 2014-01-28
  • 1970-01-01
  • 2021-12-10
  • 2013-01-13
  • 2019-02-27
  • 1970-01-01
  • 2011-01-04
  • 2013-12-01
  • 1970-01-01
相关资源
最近更新 更多