我message通過 http 協議獲取物件。該物件具有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變數回傳 value null。而我解決不了。有沒有人有解決方案?
uj5u.com熱心網友回復:
你的正則運算式不正確。請先使用在線工具測驗所有模式,例如:https : //debuggex.com
至于這一點,我必須向您說明,Node Js 實作的所有 API 都是多平臺的,除非該命令特定于特定的作業系統。但在正則運算式的情況下,所有作業系統都支持它。
此外,當您想使用建構式構建正則運算式時,其引數必須是字串。
let regex1 = /(\w )/gi;
//or
let regex2 = new RegExp("(\\w )", "gi")
uj5u.com熱心網友回復:
它們只能在行尾不同:"\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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/408736.html
標籤:
上一篇:在ReactJS中查看后端資料
下一篇:無法正確訪問聯結表上的其他欄位
