【问题标题】:Regex (node js) works on linux but not on window 10正则表达式(节点 js)适用于 linux 但不适用于窗口 10
【发布时间】:2022-01-11 16:59:14
【问题描述】:

我通过 http 协议获取对象 message。 此对象具有body 属性。

message.body: 'Denversaurus $24.90' +
    'Bread with sesame, artisan meat, cheddar, Jurassic sauce, mustard and honey, lettuce, tomato and pickles.'

我将以下正则表达式应用于该变量:

const regex = new RegExp(/([\w\dà-ú'" ]+)R\$ ([\d+,\.]+)[\n\r]([ \w\dà-ú'"\.,\+\*\(\)]+)/)
const arrayMatch = regex.exec(message.body)

在 linux 上,我得到了预期的结果,但在 Windows 上,arrayMatch 变量返回值 null。而我解决不了。 有人有解决办法吗?

【问题讨论】:

  • 我在 Linux 和 Windows 上都收到 null
  • 如果那个正则表达式真的有效,1)你的字符串不是你共享的,2)最有可能的修复是/([\wà-ú'" ]+)R\$ ([\d+,.]+)[\n\r]+([ \wà-ú'".,+*()]+)/

标签: javascript node.js regex


【解决方案1】:

您的正则表达式不正确。请先使用在线工具测试所有模式,例如: https://debuggex.com

至于这一点,我必须向您说明,Node Js 实现的所有 API 都是多平台的,除非该命令特定于特定的操作系统。但是对于正则表达式,所有操作系统都支持它。

另外,当你想用构造函数构建一个正则表达式时,它的参数必须是一个字符串。

let regex1 = /(\w+)/gi;
//or
let regex2 = new RegExp("(\\w+)", "gi")

【讨论】:

  • (注意 ES5+ ctor 可以采用正则表达式,ES6 在正则表达式 ctor 表单中添加了采用标志。我不会,但这是允许的。developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • @DaveNewton 谢谢✌
  • 您必须在 RegExp 构造函数中像这样对反斜杠进行双重转义,否则该模式将与匹配 1 个或多个 w 字符的 /w+/gi 相同。
  • @Thefourthbird 好的,谢谢✌
【解决方案2】:

它们只能在行尾不同:"\n" 是 Linux 风格,"\r\n" 是 Windows 风格。

使用

const regex = new RegExp(/([\wà-ú'" ]+)R\$ ([\d+,\.]+)[\n\r]+([ \wà-ú'"\.,\+\*\(\)]+)/)

解释

--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [\wà-ú'" ]+              any character of: word characters (a-z,
                             A-Z, 0-9, _), 'à' to 'ú', ''', '"', ' '
                             (1 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  R                        'R'
--------------------------------------------------------------------------------
  \$                       '$'
--------------------------------------------------------------------------------
                           ' '
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    [\d+,\.]+                any character of: digits (0-9), '+',
                             ',', '\.' (1 or more times (matching the
                             most amount possible))
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  [\n\r]+                  any character of: '\n' (newline), '\r'
                           (carriage return) (1 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (                        group and capture to \3:
--------------------------------------------------------------------------------
    [ \wà-                    any character of: ' ', word characters
    ú'"\.,\+\*\(\)]+         (a-z, A-Z, 0-9, _), 'à' to 'ú', ''',
                             '"', '\.', ',', '\+', '\*', '\(', '\)'
                             (1 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
  )                        end of \3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多