This article discusses how to remove a part of a string until the occurrence of a particular character. The article also caters to removing a part of the string until a specific character’s first and last occurrence.

Table of Contents:-

Javascript string remove until the first occurrence of a character

Using substring() and indexOf():-

Javascript’s substring() method returns a subset of the string between the start and end indexes or to the end of the string. 

The indexOf(searchValue, indexPosition) method in javascript gets the index of the first occurrence of the specified substring within the string. Here,

  • The searchValue specifies the substring to be searched within a string.
  • The indexPosition is optional and specifies the index from where to begin the search.

Example:-

Remove the part before the first occurrence of hyphen (-) from the string “This is a – dummy string- For example only”

Code:-

let dummyString = "This is a - dummy string- For example only";
console.log(dummyString.substring(dummyString.indexOf("-") + 1));

Explanation:-

The above code is using the substring() method to extract a substring from the original string. The substring will be formed starting from the character after the first hyphen (-) till the last character of the string.

Output:-

 dummy string- For example only

Using Regular Expression and exec():-

RegExp is the regular expression object. These objects are patterns used to match character combinations in strings.

The exec() javascript method will perform a search to match the regular expression within a specified string. The exec() method returns a result array or null if nothing is found.

Example:-

Remove the part before the first occurrence of hyphen (-) from the string “This is a – dummy string- For example only”

Code:-

let dummyString = "This is a -dummy string- For example only"
console.log(/-(.+)/.exec(dummyString)[1])

Explanation:-

The above code uses the exec() method to remove the string before the first occurrence of character hyphen (-).

The regular expression is /-(.+)/ where :-

  • / Identifies the beginning and the end of the expression.
  • (.+) Matches the string after the first occurrence of the character hyphen (-) where (.+) means at least one.

Output:-

dummy string- For example only

Javascript string remove until the last occurrence of a character

Using split() and pop():-

The split() method in javascript returns an array of substrings formed by splitting a given string.

The pop() method removes the last element of the array and returns this element.

Example:-

Remove the part before the last occurrence of (-) from the string “This is a – dummy string- For example only”

Code:-

let dummyString = "This is a - dummy string- For example only"
console.log(dummyString.split("-").pop(""))

Explanation:-

The above code uses the split() method to divide the dummyString into array elements where division is based on hyphen (-). The array elements will be {This is a, dummy string, For example only}.

The javascript’s pop() method will return the last element of the array and finally, For example only gets printed on the console.

Output:-

 For example only

Using substring() and lastIndexOf():-

The lastIndexOf(searchValue) is used to get the index of the last occurrence of the specified substring within the string. The method will return -1 if the value is not found. The parameter searchValue specifies the substring / character one needs to search.

Example:-

Remove the part before the last occurrence of (-) from the string “This is a – dummy string- For example only”

Code:-

let dummyString = "This is a - dummy string- For example only";
console.log(dummyString.substring(dummyString.lastIndexOf("-") + 1));

Output:-

 For example only

Read More:

We hope this article helped you delete a part of the string until a specific character’s occurrence. Good Luck !!!