Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
Extracting `<key>:<value>` pairs with regex

there is a textarea and want to extract with key:value (something like below image)

enter image description here

I have a regex but not working as expected

/([^\s][a-zA-Z!]+:(\s)?"?([a-z0-9\s.]+)"?[^ $])/gi

if the user enters the below string at that time regex break the group key:value.

is:"browser" browser: "chrome 11.11 V" node: error type:"Error"

expected group:

is:"browser"
browser: "chrome 11.11 V"
node: error 
type:"Error"
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.
about 4 years ago · Juan Pablo Isaza Report

0

Regex:

/([a-zA-Z!]+:\s*[" ]?([a-z0-9\s.]+)[" $])/gi

Text:

sdfd:"dsffs" dsfd: "dsf demo" sfd: dsf fdsff:"fdsfsdf"

List:

sdfd:"dsffs"
dsfd: "dsf demo"
sfd: dsf 
fdsff:"fdsfsdf"
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!