簡體   English   中英

引號內的正則表達式特定數字

[英]Regex specific number inside quote

我是正則表達式的新手,有這個返回text的 cdn url,我想使用 javascript 來匹配和提取版本號。 我可以匹配 latestVersion 但我不確定如何獲取其中的值。

例如text

...oldVersion:"1.2.0",stagingVersion:"1.2.1",latestVersion:"1.3.0",authVersion:"2.2.2"...

我嘗試執行此行以顯示latestVersion:"1.3.0但未成功

const regex = /\blatestVersion:"*"\b/
stringIneed = text.match(regex)

我只需要1.3.0不包括字符串latestVersion:

有很多方法可以做到這一點。 這是一:

 const text='...oldVersion:"1.2.0",stagingVersion:"1.2.1",latestVersion:"1.3.0",authVersion:"2.2.2"...'; console.log(text.match(/latestVersion:"(.*?)"/)?.[1])
.*? 是一個“非貪婪”通配符,它將匹配盡可能少的字符以使整個正則表達式匹配。 因此,它將在"之前停止匹配。

嘗試添加捕獲組()以匹配 Regex 中的某些字符串。

/\blatestVersion:"([0-9.]+)"/

您可以像這樣使用后視或捕獲組:

 const str = '...oldVersion:"1.2.0",stagingVersion:"1.2.1",latestVersion:"1.3.0",authVersion:"2.2.2"...' console.log( str.match(/(?<=latestVersion:")[^"]+/)?.[0] ) console.log( str.match(/latestVersion:"([^"]+)"/)?.[1] )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM