Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To remove prefix and suffix from string in javascript, use replace() method it will return a string without your prefix and suffix but it will work only when you know the prefix and suffix, suppose if you don’t know the coming prefix and suffix then you have to put some separator at the time of creating your string like “prefix###yourstring###suffix”.

Let’s see a short example of javascript removing prefixes and suffixes from string in javascript.

const string = "prefixyourstringsuffix";

console.log(string.replace("prefix", "").replace("suffix", ""));

Today, I’m going to show you How do I remove prefix and suffix in javascript, as above mentioned, I’m going to use explain both methods.

  1. remove fixed prefix and suffix from your string
  2. remove dynamic prefix and suffix from your string

Let’s start today’s tutorial on how do you remove prefix and suffix in javascript.

Javascript remove prefix and suffix

Here, we will do

  1. Take a sample string
  2. Use the replace() method for fixed prefix and suffix
  3. Use the split() method to remove dynamic prefix and suffix

const str1 = "prefixyourstringsuffix";

// Use `replace()` method for fixed prefix and suffix ( when you know prefix and suffix )
console.log(str1.replace("prefix", "").replace("suffix", ""));

const str2 =  "prefix###yourstring###suffix";

// Use the `split()` method to remove dynamic prefix and suffix ( when you don't know prefix and suffix )
console.log(str2.split("###")[1])

// Output:
// yourstring
// yourstring

Output

I hope it helps you, All the best đź‘Ť.