简体   繁体   中英

A RegEx to split by semicolon outside single or double quotes

I need a JavaScript RegEx to split a string by semicolon outside single or double quotes.

Actually i'm using the following regex /(?;\B['"][^'"]*)?(?![^'"]*['"]\B)/gm that sadly doesn't cover every case.

What i need:

const string = `Lorem ipsum; "dolor sit; amet"; consectetur 'adipiscing; elit.' Fusce; sit amet ligula.; Phasellus in laoreet quam.`;

const resultArr = string.split(/THEREGEX/gm);

console.log(resultArr);
// ["Lorem ipsum", "\"dolor sit; amet\"", " consectetur 'adipiscing; elit.' Fusce", "sit amet ligula.", " Phasellus in laoreet quam."]

You may use this regex:

((?:[^;'"]*(?:"(?:\\.|[^"])*"|'(?:\\.|[^'])*')[^;'"]*)+)|;

RegEx Demo

Code:

 const s = `Lorem ipsum; "dolor sit; amet"; consectetur 'adipiscing; elit.' Fusce; sit amet ligula.; Phasellus in laoreet quam.` const re = /((?:[^;'"]*(?:"(?:\\.|[^"])*"|'(?:\\.|[^'])*')[^;'"]*)+)|;/ console.log( s.split(re).filter(Boolean) )

RegEx Details:

  • ( : Start capture group #1
    • [^;'"]* : Match 0 or more any character that are not ' and " and not ;
    • (?: : Start non-capture group
      • "(?:\\.|[^"])*" : Match a double quoted substring ignoring all escaped quotes
      • | : OR
      • '(?:\\.|[^'])*' : Match a single quoted substring ignoring all escaped quotes
    • ) : End non-capture group
    • [^;'"]* : Match 0 or more any character that are not ' and " and not ;
  • ) : End capture group #1
  • | : OR
  • ; : Match a ;
  • .filter(Boolean) : is used to remove empty results from split array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM