Remove everything after specific Character in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Remove everything after specific Character #

To remove everything after a specific character in a string:

  1. Use the String.split() method to split the string on the character.
  2. Access the array at index 0.
  3. The first element in the array will be the part of the string before the specified character.
index.js
const str = 'BMW[1996]'; const result = str.split('[')[0]; console.log(result); // 👉️ BMW

We used the String.split method to split on the bracket [ character.

The split() method returned an array of 2 substrings - the parts of the string before and after the character.

index.js
const str = 'BMW[1996]'; const split = str.split('['); console.log(split) // 👉️ ['BMW', '1996]' console.log(split[0]) // 👉️ 'BMW'
We only need the substring before the specified character, so we accessed the array at index 0.

We can also use array destructuring to assign the value before the specified character to a variable in a single statement.

index.js
const str = 'BMW[1996]'; const [first, second] = str.split('['); console.log(first); // 👉️ 'BMW' console.log(second); // 👉️ '1996]'

The array destructuring assignment can be used to assign the values in an array to variables in a single statement.

An alternative approach is to use the slice method.

Use the String.slice() method to remove everything after a specific character, e.g. const removed = str.slice(0, str.indexOf('['));. The slice method will return the part of the string before the specified character.

index.js
const str = 'BMW[1996]'; const result = str.slice(0, str.indexOf('[')); console.log(result); // 👉️ BMW

We passed the following arguments to the String.slice method:

  1. start index - the index of the first character to be included in the returned string
  2. end index - go up to, but not including this index

The String.indexOf method returns the index of the provided character in the string or -1 if the character wasn't found in the string.

Depending on your use case, you might have to handle the scenario where the indexOf method returns -1. Passing -1 as the second parameter to the slice method would cause a bug in the application.

If you're unsure whether the string contains the specific character, use an if statement.

index.js
let result; const index = str.indexOf('['); if (index !== -1) { result = str.slice(0, index); } console.log(result); // 👉️ BMW

We only assign a string to the result variable if we're sure that the character was found in the string.

The indexOf method returns the index of the first occurrence of a character in the string. If you need to remove everything after the last occurrence of a specific character, use the lastIndexOf method.
index.js
const str = 'BMW[abc][1996]'; const result = str.slice(0, str.lastIndexOf('[')); console.log(result); // 👉️ BMW[abc]

We have two sets of brackets in the string and we only want to remove everything after the last set of brackets.

The String.lastIndexOf method returns the index of the last occurrence of the provided character or -1 if the character is not contained in the string.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.