Javascript: Remove substring from start of a string

We often come across a requirement to remove a substring from the start of a string to process it. For example, remove Mr or Mrs from names. This article will illustrate simple ways to remove a prefix from a string using different methods and examples.

Table of Contents:-

Remove substring from start of a string in javascript using replace()

Example:-

Remove ‘Mr’ or ‘Mrs’ from the strings

  • Mr George is this popular.
  • “Mrs George is this popular.

Code:-

let dummyString1 ="Mr George is popular"
let dummyString2 ="Mrs George is popular"
dummyString1 = dummyString1.replace(/(^MRS|MR)/gi, "")
dummyString2 = dummyString2.replace(/(^MRS|MR)/gi, "")
console.log(dummyString1)
console.log(dummyString2)

Explanation:-

Here, we are using the replace() javascript method that looks for Mrs / Mr at the beginning of the string and replaces it with (“”), which means nothing. Regular Expression /(^MRS|MR)/i is passed as the first argument. Where,

  • / and / mark the beginning and end of the pattern.
  • ^MRS|MR  specifies to match either Mrs or Mr at the beginning of the string
  • i specifies to replace irrespective of the case.

Output:-

 George is this popular
 George is this popular

Remove substring from start of a string in javascript using substring()

Example:-

Remove ‘This’ from the string “This language is popular”

Code:-

let dummyString ="This language is popular"
let substring ="This"
dummyString = dummyString.substring(substring.length)
console.log(dummyString)

Explanation:-

The above code uses Javascript’s substring(startIndex, endIndex) method to extract a substring from the original string based on the start and end indexes passed as parameters. The index is zero-based.

  • The startIndex specifies from where the extracted substring should begin. In our case, the value of startIndex is the length of the provided substring (‘This’). The character at startIndex is included in the new string formed.
  • The endIndex specifies till which character the extracted substring will be formed, excluding the character at the endIndex.The endIndex value is optional. Since the endIndex is not present, the new substring extracted from the original string will be formed till the last character of the original string.

Output:-

 language is popular

Remove substring from start of a string in javascript using slice()

Example:-

Remove ‘This’ from the string “This language is popular”

Code:-

let dummyString ="This language is popular"
let substring = "This"
dummyString = dummyString.slice(substring.length)
console.log(dummyString)

Explanation:-

The above code uses Javascript’s slice(startIndex, endIndex) method to return a new string part of the original string. The index is zero-based.

  • The startIndex specifies from where the extraction should begin. In our case, the value is the length of the provided substring (‘This’).
  • The endIndex specifies that the extraction will end just before the endIndex and is an optional value. In our case, the endIndex is not present; therefore, the new substring will be formed till the end of the original string. 

Output:-

 language is popular

Note that the difference between the substring() and slice() is that :

  • If startIndex > stopIndex , then in that case, substring() will swap both the arguments while slice() will return an empty string
  • If startIndex or stopIndex is negative, it is treated as 0 by substring() while slice() has another way of handling it. If startIndex is negative -> with slice() it is treated as string.length +startIndex , if endIndex is negative -> it is treated as string.length + endIndex

Read More:

We hope this article helped you to delete a substring from the start of a string in javascript. Good Luck !!!

1 thought on “Javascript: Remove substring from start of a string”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top