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

213
Views
JS Regex - non capture optional quotes

Struggling getting a working regex that doesn't capture quotes which are optional.

Code and jsfiddle below.

https://jsfiddle.net/zx7Lurob/1/

var txt = `#VER B 2000 20190403 "Text" 20190413`; //without quote 2000
var txt2 = `#VER B "2000" 20190403 "Text" 20190413`; //with qutoe 2000

var myRegexp = /(#VER)\s+"(.+)"|(.+)\s+(.+?)\s+(\d{8})\s+"(.+)"/g;

do {
    var match = myRegexp.exec(txt2);
    if (match != null)
    {
        match.forEach(function(value) {
        console.log(value);
    });
    //console.log(match);
  }
} while (match != null);

Gives a result with quoted values for txt2.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use

var myRegexp = /(#VER.*?)\s+(?:"([^"]*)"|(\S+))\s+(\d{8})\s+"([^"]*)"/g;

See the regex demo. Details:

  • (#VER.*?) - Group 1: #VER and then any zero or more chars, other than line break chars, as few as possible
  • \s+ - one or more whitespaces
  • (?:"([^"]*)"|(\S+)) - either ", then any zero or more chars other than " (captured into Group 2), or one or more non-whitespace chars (captured into Group 3),
  • \s+ - one or more whitespaces
  • (\d{8}) - Group 4: eight digits
  • \s+ - one or more whitespaces
  • "([^"]*)" - ", then any zero or more chars other than " (captured into Group 5), and then a " char.

See the JavaScript demo:

var txt = `#VER B 2000 20190403 "Text" 20190413`;
var txt2 = `#VER B "2000" 20190403 "Text" 20190413`;

var myRegexp = /(#VER.*?)\s+(?:"([^"]*)"|(\S+))\s+(\d{8})\s+"([^"]*)"/g;

for (var txt of [txt, txt2]) {
  do {
    var match = myRegexp.exec(txt);
    if (match != null)
    {
        console.log(txt, '=>',
             [match[1], match[3] || match[2], match[4], match[5]]);
    }
  } while (match != null);
}

about 4 years ago · Juan Pablo Isaza Report

0

Its difficult to determine if a positional way points are intended or
a field delimiter.
Its my guess its actually a hybrid of both.

In that case a simple matching an optional quote on both sides of a
field and outside of the capture should do the trick.

/(\#VER.*)[ \t]+"?(.+?)"?[ \t]+(\d{8})[ \t]+"(.+)"/

https://jsfiddle.net/Lj4rzo2s/

 ( \#VER .* )                  # (1)
 [ \t]+ 
 
 "?
 ( .+? )                       # (2)
 "?
 [ \t]+ 
 
 ( \d{8} )                     # (3)
 [ \t]+ 
 
 "
 ( .+ )                        # (4)
 "
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!