Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

92
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda