Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda