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

95
Visualizações
Cut multiline string with RegEx and javascript

I have example text:

var text = `class Eee {
   test(){
     console.log("123");
   }
}
use(123, Eee);

class Ttt {
   test(){
     console.log("123");
   }
}
use(456, Ttt);


class Ooo {
   test(){
     console.log("123");
   }
}
use(111, Ooo);
`;

And I wont get part text for example:

`class Ttt {
   test(){
     console.log("123");
   }
}
use(456, Ttt);`

If I use RegEx:

let result = text.match(/^class Ttt \{(.*)/gm); 

I have result: [ 'class ttt {' ]

If I use RegEx:

let result = text.match(/^class Ttt \{(.*)\}/gm); 

or

let result = text.match(/^class Ttt \{(.*)use\([\b].Ttt\);/gm);

I have result: null. How can I get the entire piece of text I want, not the first line?

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

0

You specified where the match should start, but you also have to specify where is should end.

If the end for example is at the start of a new line and the next line should be use(456, Ttt); on it's own:

^[^\S\n]*class Ttt {[^]*?\n\s*}\s*\n\s*use\(.*\);$

Note that \s can also match newlines.

The pattern in parts:

  • ^ Start of string
  • [^\S\n]* Match optional whitespace chars without newlines
  • class Ttt { Match literally
  • [^]*? Match any character including newlines, as few as possible
  • \n\s*}\s* Match a newline and } between optional whitespace chars
  • \nuse\(.*\); Match a newline and use(...);
  • $ End of string

Regex demo

var text = `class Eee {
   test(){
     console.log("123");
   }
}
use(123, Eee);

class Ttt {
   test(){
     console.log("123");
   }
}
use(456, Ttt);


class Ooo {
   test(){
     console.log("123");
   }
}
use(111, Ooo);
`;

const regex = /^[^\S\n]*class Ttt {[^]*?\n\s*}\s*\n\s*use\(.*\);$/m;
const m = text.match(regex);

if (m) {
  console.log(m[0]);
}

about 4 years ago · Juan Pablo Isaza Relatório

0

For what it's worth, here's a non-regex version:

'class ' + text.split('class ')[1]

var text = `class Eee {
   test(){
     console.log("123");
   }
}
use(123, Eee);

class Ttt {
   test(){
     console.log("123");
   }
}
use(456, Ttt);


class Ooo {
   test(){
     console.log("123");
   }
}
use(111, Ooo);
`;

console.log('class ' + text.split('class ')[1])

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