You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写正则表达式分割文本,忽略引号内的分隔符

Solution for Splitting with Delimiters Ignoring Quoted Content

Got it, let's work through this regex problem you're stuck on. The core challenge here is splitting your string on , or ;, but ignoring any delimiters that are inside double quotes—plus handling spaces inside quotes while trimming extra spaces outside them. Here are two reliable approaches that’ll give you the exact output you want:

Approach 1: Split with a Lookahead Regex

This method uses a positive lookahead to ensure we only split on delimiters that exist outside of paired double quotes. We’ll also include any trailing spaces after delimiters to avoid extra whitespace in the results.

The Regex

/(?=(?:[^"]*"[^"]*")*[^"]*$)[;,]\s*/

Breakdown of the Regex

  • [;,]\s*: Matches our target delimiters (,, ;) plus any following whitespace (so we don’t end up with leading spaces in split items).
  • (?=(?:[^"]*"[^"]*")*[^"]*$): The critical positive lookahead that checks if we’re outside quotes:
    • (?:[^"]*"[^"]*")*: Matches zero or more pairs of quotes with any non-quote characters in between (covers quoted sections).
    • [^"]*$: Ensures any remaining characters after these pairs don’t include unclosed quotes.

Example Code (JavaScript)

const testStr1 = 'coffee, water; tea';
const testStr2 = '"coffee, black;", water; tea';
const splitRegex = /(?=(?:[^"]*"[^"]*")*[^"]*$)[;,]\s*/;

console.log(testStr1.split(splitRegex)); 
// Output: ["coffee", "water", "tea"]

console.log(testStr2.split(splitRegex)); 
// Output: ["coffee, black;", "water", "tea"]

Approach 2: Match Target Content Directly

Instead of splitting, we can write a regex that extracts exactly the content we want—either quoted sections (with their internal spaces/delimiters) or non-delimiter text. This might feel more intuitive for some use cases.

The Regex

/"[^"]+"|[^;,]+/g

Breakdown of the Regex

  • "[^"]+": Matches entire quoted strings, including any internal spaces, commas, or semicolons (since [^"]+ captures everything except the closing quote).
  • |: Acts as an "or" operator.
  • [^;,]+: Matches one or more characters that aren’t delimiters (our regular, unquoted fields).
  • g: Global flag to find all matches in the string.

Example Code (JavaScript)

const testStr1 = 'coffee, water; tea';
const testStr2 = '"coffee, black;", water; tea';
const matchRegex = /"[^"]+"|[^;,]+/g;

// Use .trim() to clean up any leading/trailing whitespace in unquoted items
console.log(testStr1.match(matchRegex).map(item => item.trim()));
// Output: ["coffee", "water", "tea"]

console.log(testStr2.match(matchRegex).map(item => item.trim()));
// Output: ["coffee, black;", "water", "tea"]

Important Notes

  • Both regexes assume your input has properly paired double quotes. If there’s an unclosed quote, the behavior might not match expectations (since the lookahead/match logic relies on balanced quotes).
  • If you need to handle single quotes too, you can adjust the regex to include them (e.g., swap " with ['"] and adjust the character sets accordingly).

内容的提问来源于stack exchange,提问作者m0meni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:28:40