好久不見,第一次發貼!提前感謝任何可以提供幫助的人。
我正在使用 Node.JS 和 Electron 制作一個小型桌面應用程式,用于管理游戲檔案中的值(來自自定義檔案型別,基本上是一個美化的 INI 檔案)。讓人們在一個漂亮的 UI 中自定義檔案,而不是梳理數百行代碼。以下是檔案中的示例:
setting $Low
prop $Config A_STRING_HERE "5, 10, 15, 20"
setting $Medium
prop $Config A_STRING_HERE "10, 20, 40, 80"
setting $High
prop $Config A_STRING_HERE "20, 40, 80, 160"
setting $VeryHigh
prop $Config A_STRING_HERE "40, 80, 160, 320"
例如,我將如何匹配包含“A_STRING_HERE”的“設定 $High”(第 3 次)出現直到行尾。我希望只匹配:
A_STRING_HERE "20, 40, 80, 160"。
這些值是動態的并且會發生很大變化,因此將行與值匹配不一定是一種選擇。
\A_STRING_HERE. \g將匹配所有出現的行尾。\A_STRING_HERE. \將第一次匹配到行尾。\A_STRING_HERE. \??????將第 3 次匹配到行尾?
再次感謝,一切順利!
uj5u.com熱心網友回復:
你可以試試這個正則運算式:
(?<=setting \$High\n\s*?prop \$Config )A_STRING_HERE.*
解釋
(?<=)- 代表
顯示代碼片段
const regex = /(?<=setting \$High\n\s*?prop \$Config )A_STRING_HERE.*/g; // Alternative syntax using RegExp constructor // const regex = new RegExp('(?<=setting \\$High\\n\\s*?prop \\$Config )A_STRING_HERE.*', 'g') const str = `setting \$Low prop \$Config A_STRING_HERE "5, 10, 15, 20" setting \$Medium prop \$Config A_STRING_HERE "10, 20, 40, 80" setting \$High prop \$Config A_STRING_HERE "20, 40, 80, 160" setting \$VeryHigh prop \$Config A_STRING_HERE "40, 80, 160, 320"`; console.log(str.match(regex)[0]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/481965.html標籤:javascript 正则表达式
