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

420
Vistas
Replace everything between two words including this words

How to change this tt /* a */ pp /* b */ dd to

tt dd

this not including a and b link

var test = "tt /* a */ pp /* b */ dd";
// expected:
var res = "tt dd";

Is it simple with regexp?

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

0

If you want to remove something between two things, including those things, regex could do the trick pretty easily:

const str = 'tt /* a */ pp /* b */ dd';
const start = '/\\* a \\*/';
const end = '/\\* b \\*/';

const pattern = new RegExp(`${start}.*${end}`, 'g');

const result = str.replace(pattern, '');

console.log(pattern);
console.log(result);

There are two gotchas however.

The first is from your choice of delimiter. Since it has a * in it, which has special meaning in regex, you need to be sure to escape it. If you choose a delimiter without characters special to regex, you don't have to do that.

The other gotcha isn't a gotcha so much as it is a requirement issue. You wanted tt dd, but you'll get tt dd as you describe it (two spaces), since both spaces are outside of your delimiters.

There are a couple of ways to deal with that. One could be to just replace two or more spaces with one space:

const str = 'tt  dd';
const result = str.replace(/\s+/g, ' ');

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

If tt and dd vary, here's a regular expression that removes what's between those variances:

const re = /\s+.*\/\s+/gm;

let str = `tt /* a */ pp /* b */ dd`;
str = str.replace(re,' ');
console.log(str);  // tt dd

// Change tt and dd to aa and bb, instead, for example:
str = `aa /* a */ pp /* b */ bb`;
str = str.replace(re,' ');
console.log(str); // aa bb

If tt and dd do not vary, I see no point in using a regular expression; you'd simply (statically) set that string to the desired value.

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