I get the object message via http protocol.
This object has the body property.
message.body: 'Denversaurus $24.90'
'Bread with sesame, artisan meat, cheddar, Jurassic sauce, mustard and honey, lettuce, tomato and pickles.'
I apply the following regex to that variable:
const regex = new RegExp(/([\w\dà-ú'" ] )R\$ ([\d ,\.] )[\n\r]([ \w\dà-ú'"\.,\ \*\(\)] )/)
const arrayMatch = regex.exec(message.body)
On linux, I get the expected result, but on windows, the arrayMatch variable returns with the value null. And I can't solve it.
Does anyone have the solution?
CodePudding user response:
your regex is incorrect . please test all patterns first with online tools like : https://debuggex.com
As for this, I have to make it clear to you that all APIs implemented by Node Js are multiplatform unless the command is specific to a particular operating system. But in the case of regex, it is supported by all operating systems.
Also, when you want to build a regex with a constructor, its argument must be a string.
let regex1 = /(\w )/gi;
//or
let regex2 = new RegExp("(\\w )", "gi")
CodePudding user response:
They must only differ in line ends: "\n" is Linux-style and "\r\n" is Windows-style.
Use
const regex = new RegExp(/([\wà-ú'" ] )R\$ ([\d ,\.] )[\n\r] ([ \wà-ú'"\.,\ \*\(\)] )/)
EXPLANATION
--------------------------------------------------------------------------------
( 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
