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

190
Vistas
Getting the content between two characters

So I have this (example) string: 1234VAR239582358X

And I want to get what's in between VAR and X. I can easily replace it using .replace(/VAR.*X/, "replacement"); But, how would I get the /VAR.*X/as a variable?

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

0

I think what you are looking for might be

string.match(/VAR(.*)X/)[1] 

The brackets around the .* mark a group. Those groups are returned inside the Array that match creates :)

If you want to only replace what's in between "VAR" and "X" it would be

string.replace(/VAR(.*)X/, "VAR" + "replacement" + "X");

Or more generic:

string.replace(/(VAR).*(X)/, "$1replacement$2");
about 4 years ago · Juan Pablo Isaza Denunciar

0

If you

want to get what's in between VAR and X

then using .* would do the job for the given example string.

But note that is will match until the end of the string, and then backtrack to the first occurrence of X it can match, being the last occurrence of the X char in the string and possible match too much.

If you want to match only the digits, you can match 1+ digits in a capture group using VAR(\d+)X

const regex = /VAR(\d+)X/;
const str = "1234VAR239582358X";
const m = str.match(regex);

if (m) {
  let myVariable = m[1];
  console.log(myVariable);
}

Or you can match until the first occurrence of an X char using a negated character class VAR([^\r\nX]+)X

const regex = /VAR([^\r\nX]+)X/;
const str = "1234VAR239582358X";
const m = str.match(regex);

if (m) {
  let myVariable = m[1];
  console.log(myVariable);
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can store it as variable like this,

const pattern = "VAR.*X";
const reg = new RegExp(pattern);

Then use,

.replace(reg, "replacement");
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