Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

107
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda