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

131
Vistas
How to get specific text from a string in regex

I have a string from which I need to extract specific texts

let str = 'id = "Test This is" id ="second" abc 123 id ="third-123"';
let res = str.match(/[^id ="\[](.*)[^\]]/g);
console.log(res);

I want the texts in ids only ['Test This is','second','third-123'] But I am getting [ 'Test This is" id ="second" abc 123 id ="third-123"' ] The whole text after first id which I don't want.I need help with the pattern.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your pattern uses a negated character class where you exclude matching the listed individual characters, and also exclude matching [ and ] which are not present in the example data.

That way you match the first char T in the string with [^id ="\[] and match the last char ; in the string with [^\]] and the .* captures all in between.

I would suggest using a negated character class to exclude matching the " instead:;

\bid\s*=\s*"([^"]*)"

Regex demo

let str = 'id = "Test This is" id ="second" abc 123 id ="third-123"';
let res = str.matchAll(/\bid\s*=\s*"([^"]*)"/g);
console.log(Array.from(str.matchAll(/\bid\s*=\s*"([^"]*)"/g), m => m[1]));

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can simplify this down to a non-greedy regular expression indepedent of where the quotes fall in the string:

let str = 'id = "Test This is" id ="second" abc 123 id ="third-123"';
let res = str.match(/".*?"/g);
console.log(res);

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