Recibo el message del objeto a través del protocolo http. Este objeto tiene la propiedad del body .
message.body: 'Denversaurus $24.90' + 'Bread with sesame, artisan meat, cheddar, Jurassic sauce, mustard and honey, lettuce, tomato and pickles.'Aplico la siguiente expresión regular a esa variable:
const regex = new RegExp(/([\w\dà-ú'" ]+)R\$ ([\d+,\.]+)[\n\r]([ \w\dà-ú'"\.,\+\*\(\)]+)/) const arrayMatch = regex.exec(message.body) En Linux, obtengo el resultado esperado, pero en Windows, la variable arrayMatch regresa con el valor null . Y no puedo resolverlo. ¿Alguien tiene la solución?
su expresión regular es incorrecta. primero pruebe todos los patrones con herramientas en línea como: https://debuggex.com
En cuanto a esto, debo aclararles que todas las API implementadas por Node Js son multiplataforma a menos que el comando sea específico para un sistema operativo en particular. Pero en el caso de expresiones regulares, es compatible con todos los sistemas operativos.
Además, cuando desee crear una expresión regular con un constructor, su argumento debe ser una cadena.
let regex1 = /(\w+)/gi; //or let regex2 = new RegExp("(\\w+)", "gi")Solo deben diferir en los extremos de línea: "\n" es estilo Linux y "\r\n" es estilo Windows.
Usar
const regex = new RegExp(/([\wà-ú'" ]+)R\$ ([\d+,\.]+)[\n\r]+([ \wà-ú'"\.,\+\*\(\)]+)/)EXPLICACIÓN
-------------------------------------------------------------------------------- ( group and capture to \1: -------------------------------------------------------------------------------- [\wà-ú'" ]+ any character of: word characters (az, AZ, 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 ú'"\.,\+\*\(\)]+ (az, AZ, 0-9, _), 'à' to 'ú', ''', '"', '\.', ',', '\+', '\*', '\(', '\)' (1 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- ) end of \3