Data Extraction in JavaScript

From the JavaScript Regular Expressions cheat sheet · Common Patterns · verified Jul 2026

Data Extraction

Extracting structured data from text

javascript
// Extract all prices
"Items: $10.99, $25.00, €30.50".matchAll(/[\$€£]\d+\.?\d*/g)

// Parse CSV line
function parseCSV(line) {
  return line.match(/(".*?"|[^",\s]+)(?=\s*,|\s*$)/g)
    ?.map(val => val.replace(/^"|"$/g, ''))
}
parseCSV('name,"last, first",age')
// ['name', 'last, first', 'age']

// Extract key-value pairs
"name=John age=30 city=NYC".matchAll(/(\w+)=(\w+)/g)
// Iterate to get: ['name', 'John'], ['age', '30'], etc.

// Parse log entries
const logRegex = /^\[(?<time>[\d:]+)\] \[(?<level>\w+)\] (?<message>.+)$/gm
const logs = logText.matchAll(logRegex)
for (const match of logs) {
  console.log(match.groups)
  // {time: "10:30:45", level: "ERROR", message: "..."}
}

// Extract JSON from text
const jsonRegex = /\{(?:[^{}]|(?:\{[^{}]*\}))*\}/g
text.match(jsonRegex)?.map(json => JSON.parse(json))

// Parse function calls
"func1(arg1, arg2) func2(arg3)".matchAll(/(\w+)\(([^)]*)\)/g)
// Extract function names and arguments

// Extract code blocks from markdown
markdown.matchAll(/\`\`\`(\w+)?\n([\s\S]*?)\`\`\`/g)

// Parse URL components
const url = "https://example.com:8080/path?query=1#hash"
const urlRegex = /^(https?):\/\/([^:\/]+):?(\d+)?(\/[^?#]*)?(\?[^#]*)?(#.*)?$/
const [, protocol, host, port, path, query, hash] = url.match(urlRegex)
💡 Use matchAll() with named groups for structured extraction
⚡ Consider using dedicated parsers for complex formats
📌 Always handle null/undefined from match results
🟢 Named groups make extracted data self-documenting
extractionparsing

More JavaScript tasks

Back to the full JavaScript Regular Expressions cheat sheet