Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!