[英]regex 2 numbers from a string
我怎樣才能取出Price Our:
和以下之間的 2 個數字|
從這個字符串?
"Delivery: Price => 30.00 - 45.00 | Price Our: 1900.00 => 1800.00 | Delivery: Contact => 3 - 4 Days | "
我試過(Price Our:.*?(?=\\|))
但這就是我的正則表達式知識的極限
您可以使用 2 個捕獲組:
\bPrice Our: (\d+(?:\.\d+)?) => (\d+(?:\.\d+)?) \|
模式匹配:
\\bPrice Our:
防止部分匹配的單詞邊界,然后匹配Price Our:
(\\d+(?:\\.\\d+)?)
捕獲組1,匹配一個帶有可選小數部分的數字=>
字面匹配(\\d+(?:\\.\\d+)?)
捕獲組2,匹配一個帶有可選小數部分的數字\\|
匹配|
const s = "Delivery: Price => 30.00 - 45.00 | Price Our: 1900.00 => 1800.00 | Delivery: Contact => 3 - 4 Days | "; const regex = /\\bPrice Our: (\\d+(?:\\.\\d+)?) => (\\d+(?:\\.\\d+)?) \\|/; const match = s.match(regex); if (match) { console.log(match[1]); console.log(match[2]); }
如果數字位於字符串的末尾,並且沒有|
接下來,您可以使用交替來聲明字符串的結尾:
\bPrice Our: (\d+(?:\.\d+)?) => (\d+(?:\.\d+)?)(?: \||$)
(\\d*\\.\\d+)
regex101解釋:
const regex = /(\\d*\\.\\d+)/gm; const str = "Delivery: Contact => 3 - 4 Days | Price Our: 1900.00 => 1800.00 | Delivery: Contact => 3 - 4 Days | "; const res = str.match(regex) console.log(res)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.