You can use

const text = 'is:"browser" browser: "chrome 11.11 V" node: error type:"Error"';
const re = /(\w+):\s*(?:"([^"]*)"|(\S+))/g;
let dict = {}, m;
while(m = re.exec(text)) {
  dict[m[1]]=(m[3] || m[2]);
}
console.log(dict);

// Or just get all matches:
console.log(text.match(re))

See the regex demo. Details:

  • (\w+) - Group 1: one or more word chars
  • : - a colon
  • \s* - zero or more whitespaces
  • (?:"([^"]*)"|(\S+)) - either of
    • "([^"]*)" - ", 0+ non-commas (captured in Group 2), "
    • | - or
    • (\S+) - Group 3: one or more non-whitespaces chars.
Answer from Wiktor Stribiżew on Stack Overflow
🌐
Reddit
reddit.com › r/regex › finding key value pairs with regex
r/regex on Reddit: Finding key value pairs with regex
May 26, 2024 -

Hi,

Totally new to regex. I've tried asking chatGPT and several regex generators but I cannot figure this out.

I'm trying to extract key value pairs from specifications from a website using javascript.

Assume keys and values alternate, I am pulling the data from a table. Assume if the first character of second word is uppercase it's a key, else it's a value.

Example (raw text):

Machine washable Yes Color Clear Series Share Capacity 123 cl Category Vase Brand RandomBrand Item.nr 43140   

Example (paired manually):

Machine washable: Yes Color: Clear Series: Share Capacity: 123 cl Category: Vase Brand: RandomBrand Item.nr: 43140

Is this even possible with regex? I feel lost here.

Thanks for taking the time.

Edit: I will try another approach but Im still curious if this is possible.

Discussions

regex - Extracting numeric values from a string containing key:value pairs - Code Review Stack Exchange
3 Build a dictionary from a string by the extraction of data from all the pairs contained inside the string and clean string from TAGs More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
March 21, 2019
pcre - Extracting `<key>=<value>` pairs with regex, dot group (.+?) not cooperating with optional space (\s) - Stack Overflow
I have this string: metadata=1.2 name=stone:supershare UUID=eff4e7bc:47aea5cf:0f0560f0:2de38475 I wish to extract from it the key and value pairs: = . /(\w+)=(.+?)\s/g This... More on stackoverflow.com
🌐 stackoverflow.com
September 23, 2019
Returning value from key-value pairs
It would help to know what regex engine you're using. In Python you could do something like https://regex101.com/r/Rhlnz4/1 » import re » r = re.compile(r"(\w+) *= *([^|]*)") » s = "year=2022 | month=March| date=11 | day=Friday| time=1:00 AM" » for m in r.finditer(s): … print(f"Group {m.group(1)} is set to {m.group(2)}") … Group year is set to 2022 Group month is set to March Group date is set to 11 Group day is set to Friday Group time is set to 1:00 AM More on reddit.com
🌐 r/regex
8
2
March 23, 2022
Regex to match a specific key value pair in Logstash
I'm looking to remove specific key=value pair that are inside a STRING. Say, input event is as follows: { "ABC": "10119707", "Request_StartTime": "1558952196175", "Severity": "INFO", "UUID": "481e8cfa-399c-4996-a4d3-7e9b7ec866fa", "Src_LogMsg": "type=abc, user=abc.def, vid=1111, api=fooapi, ... More on discuss.elastic.co
🌐 discuss.elastic.co
8
0
May 29, 2019
🌐
Splunk Community
community.splunk.com › t5 › Splunk-Search › How-to-create-a-regex-to-extract-key-value-pairs › m-p › 324096
How to create a regex to extract key value pairs - Splunk Community
October 24, 2017 - Splunk picks up the key value pairs except the value with the whitespaces, for instance, "subject=my testing" from the sample log below, Splunk only extracts "my" from "subject". I can create a custom regex, such as "src=(?P[^\s]+)\sdst=(?P[^\s]+)\sspt=(?P[^\s]+)\ssubject=(?P.+)".
🌐
Medium
medium.com › @sg24 › regular-expression-notes-extracting-key-value-pairs-c3598cf5501d
Extracting Complex key-value pairs with Regular Expression - SG - InfoSecNinja - Medium
July 8, 2024 - Scenario 3— We want to capture the value but not the date which is mixing with value data. link with Sample dataset — https://regex101.com/r/SZYAGq/1
🌐
regex101
regex101.com › library › hC3fP4
Key/Value pairs with multiline support
Please complete this security check to continue to regex101.
🌐
Alteryx Community
community.alteryx.com › t5 › Alteryx-Designer-Desktop-Discussions › RegEx-Key-Value-Pair › td-p › 500855
Solved: RegEx Key Value Pair - Alteryx Community
June 13, 2024 - @rahulchauhan - you can simply change the filters to search for the KEY to return the VALUE. I just mocked up a quick example to show the JSON Parse can parse most of the data without writing RegEx. Attached is the workflow modified with filtering on Key. ... @T_Willins Awesome! This works perfectly fine. Thank you so much, sir. You're a gentleman and a scholar. ... @T_Willins Just a long shot - Do you, by any chance, know the Redshift version of the code you've given? I'm extracting this data directly from Redshift and apparently Redshift doesn't support your normal RegEx.
Find elsewhere
🌐
Alibaba Cloud
alibabacloud.com › help › en › sls › user-guide › extract-dynamic-key-value-pairs-from-a-string
How to extract dynamic key-value pairs from a string by using different functions - Simple Log Service - Alibaba Cloud Documentation Center
April 9, 2025 - In most cases, use the e_kv function and configure specific parameters to extract key-value pairs, especially when you need to extract and escape enclosed characters or backslashes (\). In complicated or advanced use cases, use the e_regex function ...
🌐
Regex Tester
regextester.com › 104222
key-value pair - Regex Tester/Debugger
Regex Tester is a tool to learn, build, & test Regular Expressions (RegEx / RegExp).
🌐
CodingTechRoom
codingtechroom.com › question › -regex-extract-key-value-pairs-spaces-in-values
How to Use Regex to Extract Key-Value Pairs with Spaces in Values? - CodingTechRoom
In JavaScript, the regex pattern can be `/([a-zA-Z0-9]+):\s*([^;]+);?/g`, where 'Key1: Value 1;' is a valid input. This pattern captures alphanumeric keys followed by a colon and one or more spaces before capturing the value, which can have spaces.
🌐
Elastic
discuss.elastic.co › elastic stack › logstash
Regex to match a specific key value pair in Logstash - Logstash - Discuss the Elastic Stack
May 29, 2019 - I'm looking to remove specific key=value pair that are inside a STRING. Say, input event is as follows: { "ABC": "10119707", "Request_StartTime": "1558952196175", "Severity": "INFO", "UUID": "481e8cfa-399c-4996-a4d3-7e9b7ec866fa", "Src_LogMsg": "type=abc, user=abc.def, vid=1111, api=fooapi, email=abc.def@gmail.com, cat=1", "@version": "1", "@timestamp": "2019-05-27T10:16:36.180Z", "Src_Host": "Hostname", "Request_IpAddress": "1.1.1.1" } I want to remove the key=value pair user=abc.de...
Top answer
1 of 2
3

You have a few ways to do this. First, since you said your key-value pairs are embedded in a larger email, you need to extract them. You can do that with this regex, which checks for a line starting with a word and an equals sign:

import re

text = " ... Full email text ... "
dataPoints = re.search(r"^\w*=.*$", text, re.MULTILINE).group(0)

Then you need to create your dictionary.

Option 1: Simplest

Use the following regex find:

result = dict(re.findall(r'(\w*)=(\".*?\"|\S*)', dataPoints))

Regex demo

Option 2: Typical split

Follow the typical method for this problem: split the various key-value combinations into a list, and then split each combination into separate keys and values. However, since your key-value pairs are separated by spaces rather than semicolons, ampersands, or something similar, and some of your values have spaces in them, we can't simply split by spaces. That means we need to use a regex lookahead for this to work properly:

regexSplit = dict([i.split("=") for i in re.split(r"\s(?=\w+=)", dataPoints)])

Option 3: No regex

If you want to avoid using regex altogether for whatever reason, you can use the following, which splits on equals signs and then recombines the keys and values into the proper arrangement for creating a dictionary:

allSplits = dataPoints.split("=")
splitList = [allSplits[0]] + [i for a in allSplits[1:-1] 
    for i in a.rsplit(" ", 1)] + [allSplits[-1]]

splitDict = dict(zip(splitList[::2], splitList[1::2]))

The code above assumes your dictionary will end up with at least 2 items.

Demo for all 3 options

2 of 2
0

What about adding an OR (|) to your regex, e.g.

(\w*)=(\"[\w\s\+()\.]*\"|[\w\-\:\.]*)

matches the string you gave.
Note

  • \"[\w\s\+()\.]*\" matches all the values enclosed in ""
  • [\w\-\:\.]* matches the ones without
🌐
TopDealsNet
ubuntuask.com › blog › how-to-parse-key-value-with-regex
How to Parse Key-Value With Regex in 2026?
September 21, 2025 - Parsing key-value pairs with regex ... can be done by constructing a regex pattern that matches the desired format of the key-value pairs, and then using capturing groups to extract the key and value components....