正则表达式多匹配字符串:提取多格式命令行标识与值
Got it, let's figure out how to build that regex you need! You want a single pattern that can parse flags and their corresponding values (when they exist) from commands like the examples you shared—covering flags with no value, plain unquoted values, quoted values (single or double quotes), even comma-separated values with extra spaces. Here's a solution that handles all these cases:
The Regex Pattern
^(\w+)(?:=(?:"([^"]*)"|'([^']*)'|([^\s]+)))?$
This regex is designed to match individual command arguments (like search or search="test, test-string" from your examples) and extract the flag and its value.
Breakdown of the Regex
Let's break down each part to understand how it works:
^and$: Anchor the match to the start and end of the argument string, ensuring we don't get partial matches.(\w+): Captures the flag name (group 1). This matches letters, numbers, and underscores—adjust to[\w-]+if you need flags to include hyphens.(?:=...): A non-capturing group that handles the optional value portion (the=sign plus the value). The?at the end makes this entire group optional, so flags without values (like justsearch) are matched.(?:"([^"]*)"|'([^']*)'|([^\s]+)): A branching group that matches three possible value formats:"([^"]*)": Matches double-quoted values, capturing everything inside the quotes (group 2).'([^']*)': Matches single-quoted values, capturing everything inside the quotes (group 3).([^\s]+): Matches unquoted values, capturing all characters until the next space (group 4)—since command arguments are space-separated.
Matching Examples
Here's how the regex works with your sample commands (we're focusing on the argument after npm start):
- Command:
npm start search
Matched argument:search
Captured groups: Group 1 =search, Groups 2-4 = empty
Result: Flag =search, No value - Command:
npm start search=test-string
Matched argument:search=test-string
Captured groups: Group 1 =search, Group 4 =test-string
Result: Flag =search, Value =test-string - Command:
npm start search="test"
Matched argument:search="test"
Captured groups: Group 1 =search, Group 2 =test
Result: Flag =search, Value =test - Command:
npm start search='test'
Matched argument:search='test'
Captured groups: Group 1 =search, Group 3 =test
Result: Flag =search, Value =test - Command:
npm start search="test, test-string"
Matched argument:search="test, test-string"
Captured groups: Group 1 =search, Group 2 =test, test-string
Result: Flag =search, Value =test, test-string - Command:
npm start search=' test,test '
Matched argument:search=' test,test '
Captured groups: Group 1 =search, Group 3 =test,test
Result: Flag =search, Value =test,test(preserves leading/trailing spaces)
Handling Multiple Comma-Separated Values
If you need to split comma-separated values into an array, once you've extracted the value (from group 2, 3, or 4), you can use a simple split with regex to handle optional spaces around commas:
// Example value from the last command: " test,test " const value = " test,test "; const valuesArray = value.trim().split(/\s*,\s*/); // Result: ["test", "test"]
内容的提问来源于stack exchange,提问作者Shannon Hochkins

