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
How to correctly check if a string match regex in javascript

I want to check if a string match the following pattern

Having a text contains between Two "and each text can be separated to another by a ; the appending ;is mantatory

I tried with the regular expression /"(.*?)";?/ but can't make it work

In the code snippet below test3 return true but should not match the regex

I don't know if it's my regex or the way I try my regex

const regex = /"(.*?)";?/;


const test = `"hello"; "world";`;
const test2 = `"hello"; "world"`;

const test3 = `"hello; "world"`;
const test4 = `"hello"; world`;
const test5 = `"hello""world"`;

//should be true
console.log("test1",regex.test(test));
console.log("test2",regex.test(test2));

//should be false
console.log("test3",regex.test(test3));
console.log("test4",regex.test(test4));
console.log("test5",regex.test(test5));

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

It appears you want to match semi-colon separated string literals, and that means you can use

const sl = String.raw`"[^"\\]*(?:\\[^][^"\\]*)*"`;
const regex = new RegExp(String.raw`^${sl}(?:\s*;\s*${sl})*;?$`);


const test = `"hello"; "world";`;
const test2 = `"hello"; "world"`;

const test3 = `"hello; "world"`;
const test4 = `"hello"; world`;
const test5 = `"hello""world"`;

//should be true
console.log("test1",regex.test(test));
console.log("test2",regex.test(test2));

//should be false
console.log("test3",regex.test(test3));
console.log("test4",regex.test(test4));
console.log("test5",regex.test(test5));

The regex is ^${sl}(?:\s*;\s*${sl})*;?$ matching

  • ^ - start of string
  • ${sl} - the string literal pattern
  • (?:\s*;\s*${sl})* - zero or more sequences of ; enclosed with zero or more whitespaces and then a string literal pattern
  • ;? - an optional ; char (add \s* if you need to match trailing whitespace right after)
  • $ - end of string.

The "[^"\\]*(?:\\[^][^"\\]*)*" pattern matches

  • " - a " char
  • [^"\\]* - zero or more chars other than " and \
  • (?:\\[^][^"\\]*)* - zero or more occurrences of \, then any char, then zero or more chars other than " and \
  • " - a " char.
about 4 years ago · Juan Pablo Isaza Relatório

0

/^"[^"]*"(?:\s*;\s*"[^"]*")+;?$/
  ---2---
         ---------3---------
                            -4

... can be broken down into ...

  1. /^ ... $/
  2. "[^"]*"
  3. (?:\s*;\s*"[^"]*")+
  4. ;?

... which then translates to ...

  1. In between new line start and line end ...
  2. match a double quote, followed by an optional sequence of characters, each character not being a double quote, followed by a double quote.
  3. group ( ... ) the following match but do not capture it (?: ... ) and force this pattern to be present at least once (?: ... )+ ... within this group ...
    • match the sequence of an optional whitespace (WS) a semicolon and another optional WS followed by the pattern already described with/under (2).
  4. the entire match is allowed to end with none or exactly one semicolon.

const regex = /^"[^"]*"(?:\s*;\s*"[^"]*")+;?$/;

const test1 = `"hello"; "world";`;
const test1a = `"hello" ; "world" ;"world"; "world"`;
const test2 = `"hello" ; "world"`;
const test2a = `"hello";"world" ; "world";"world"`;

const test3 = `"hello; "world"`;
const test4 = `"hello"; world`;
const test5 = `"hello""world"`;


const test6 = `"hello ; world;";"hello ;world"`;

const test6a = `"hello ;world;" "hello; world"`;
const test6b = `"hello; world"`;


//should be true
console.log("test1", regex.test(test1));
console.log("test1a", regex.test(test1a));
console.log("test2", regex.test(test2));
console.log("test2a", regex.test(test2a));

console.log("test6", regex.test(test6));

//should be false
console.log("test3", regex.test(test3));
console.log("test4", regex.test(test4));
console.log("test5", regex.test(test5));

console.log("test6a", regex.test(test6a));
console.log("test6b", regex.test(test6b));
.as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

It match the first two cases:

\"(.*)?\";?\s{1}\"(.*)?\";?

Example:

const regex = /\"(.*)?\";?\s{1}\"(.*)?\";?/;


const test = `"hello"; "world";`;
const test2 = `"hello"; "world"`;

const test3 = `"hello; "world"`;
const test4 = `"hello"; world`;
const test5 = `"hello""world"`;

//should be true
console.log("test1",regex.test(test));
console.log("test2",regex.test(test2));

//should be false
console.log("test3",regex.test(test3));
console.log("test4",regex.test(test4));
console.log("test5",regex.test(test5));

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