簡體   English   中英

匹配單引號、雙引號或根本沒有引號之間的文本

[英]Match text between single quotes, double quotes, or no quotes at all

我正在嘗試解析類似 CLI 的 arguments 可以用單引號、雙引號或根本沒有引號括起來。
這是我想要得到的一個例子:

// --message "This is a 'quoted' message" --other 'This uses the "other" quotes'
const str = "--message \"This is a 'quoted' message\" --other 'This uses the \"other\" quotes'"

matchGitArgs(str) // ['--message', 'This is a \'quoted\' message', '--other', 'This uses the "other" quotes']

我發現了很多類似的問題,所以這就是它與它們不同的原因:

  • 重要的是它匹配 arguments 而不是引號,並保持原始順序
  • 它應該能夠解析同一字符串中的單引號和雙引號 arguments
  • 它不應該與引號本身匹配:
matchGitArgs('This is "quoted"')
// Correct: ['This', 'is', 'quoted']
// Wrong: ['This', 'is', '"quoted"']
  • 它應該允許其中包含轉義引號和其他引號:
matchGitArgs('It is "ok" to use \'these\'')
// ["It", "is", "ok", "to", "use", "these"]

我嘗試使用在這里找到的許多不同的正則表達式模式,但它們都不滿足其中一個條件。 我也嘗試過使用旨在解析 CLI arguments 的庫,但似乎它們都依賴於process.argv (在 Node.js 中),它已經根據引號正確拆分,所以對我沒有幫助。
我基本上需要做的是生成一個類似process.argv的數組。

它不需要是一個單一的正則表達式,一個 js/ts function 也可以。

“詳細”表達式和命名組對於標記問題特別有效:

 function* parseArgs(cmdLine) { const re = String.raw` ( -- (?<longOpt> \w+) (\s+ | =) ) | ( - (?<shortOpt> \w+) \s+ ) | ( (' (?<sq> (\\. | [^'])* ) ') \s+ ) | ( (" (?<dq> (\\. | [^"])* ) ") \s+ ) | ( (?<raw> [^\s"'-]+) \s+ ) | (?<error> \S) `.replace(/\s+/g, ''); for (let m of (cmdLine + ' ').matchAll(re)) { let g = Object.entries(m.groups).filter(p => p[1]); let [type, val] = g[0]; switch (type) { case 'error': throw new Error(m.index); case 'sq': case 'dq': yield ['value', val.replace(/\\/g, '')]; break; case 'raw': yield ['value', val]; break; case 'longOpt': case 'shortOpt': yield ['option', val]; } } } // args = String.raw` --message "This is \"a\" 'quoted' message" -s --longOption 'This uses the "other" quotes' --foo 1234 --file=message.txt --file2="Application Support/message.txt" ` for (let [type, s] of parseArgs(args)) console.log(type, ':', s)

暫無
暫無

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

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