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

82
Visualizações
Javascript regex to match a block of code starting with a given pattern

So I'm working with a javascript script and I need a quick way to match "JSDocs" using Regex. The pattern I'm trying to match is this.

# this is block1
/// text
/// text
/// text
/// text

# this is block2
/// text
/// text
/// text
/// text

so a block is defined by consecutive lines starting with "///" what I have so far is:

/(/^(\/{3}[\w\W]*)^(?!\/{3})/gm

so I'm starting at a line starting with /// and then matching until a line doesn't start with /// but for this to work I need to have two \n\n at the end of the input block and I don't want that.

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

0

You can use

const result = text.match(/^\/{3}.*(?:\r?\n\/{3}.*)*/gm)

See the regex demo. Note the g and m flags. g will match all occurrences of the pattern in the string and m will make ^ match start of any line.

Details:

  • ^ - start of a line
  • \/{3} - three slashes
  • .* - the rest of the line
  • (?:\r?\n\/{3}.*)* - zero or more sequences of a line ending (CRLF or LF) and the rest of the line.

See the JavaScipt demo:

const text = '# this is block1\n/// text\n/// text\n/// text\n/// text\n\n# this is block2\n/// text\n/// text\n/// text\n/// text';
const result = text.match(/^\/{3}.*(?:\r?\n\/{3}.*)*/gm)
console.log(result);

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