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

101
Vistas
Search for one or more characters and replace in JavaScript

By using indexOf() I was able to detect if the input contains "SP-" and replace.

However, I need to look for more than one set of characters: sp-, SP-, eb-, EB- and more...

I have the following to replace sp- and SP- but I don't want to replicate this entire block for every instance.

// Check for 'sp-' characters in the order ID.
if (order_id.indexOf("SP-") !== -1) {
  // Remove string from input
  form.find('input[name="orderid"]').val(order_id.replace("SP-", ""));
}

// Check for 'SP-' characters in the order ID.
if (order_id.indexOf("sp-") !== -1) {
  // Remove string from input
  form.find('input[name="orderid"]').val(order_id.replace("sp-", ""));
}

Update - Just thought of a better solution, find all characters before and including - and remove it. So we're not limited to a specific list in case new prefixes are added at a later date.

sp-1234, SP-1234, xx-1234, etc.

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

0

To replace all case insensitive can you try this

form.find('input[name="orderid"]').val(order_id.replace(/sp-/gi, ''));

or you can do something like this

['SP-', 'sp-', 'eb-', 'EB-'].forEach((item)=>{
   if ( order_id.indexOf(item) !== -1 ) {
        // Remove string from input
        form.find('input[name="orderid"]').val(order_id.replace(item, ''));
    }
});

Update - Just thought of a better solution, find all characters before and including - and remove it. So we're not limited to a specific list in case new prefixes are added at a later date.

sp-1234, SP-1234, xx-1234, etc.

Maybe something like this should work

if(order_id.indexOf('-') !== -1) {
 var prefix = order_id.substr(0, order_id.indexOf('-'));
 form.find('input[name="orderid"]').val(order_id.replace(`${prefix}-`, ""));
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Just use a regular expression with case insensitivity set. There is no reason to check for the string exists inside the string because the replace method does not do anything if there is no match.

function removePrefix(str) {
  return str.replace(/(sp|eb)-/i, '');
}


["fo-foooo", "sp-123", "SP-123", "eb-321", "EB-911"].forEach( function (str) {
  console.log(str, removePrefix(str));
});

Matching anything that starts with a string followed by a dash

function removePrefix(str) {
  return str.replace(/^[^-]+-/i, '');
}

["fo-foooo", "sp-123", "SP-123", "eb-321", "EB-911"].forEach( function (str) {
  console.log(str, removePrefix(str));
});

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try something like this:

var vals = ["sp-","ep-"] // Put all the values here

vals.forEach(v => {
  if (order_id.indexOf(v) !== -1) {form.find('input[name="orderid"]').val(order_id.replace(v, ''))};
})
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