How to Trim Substring in JavaScript

To trim a substring in JavaScript:

  1. replace(): It is a  method in JavaScript to remove a particular substring from a given string.
  2. substr(): It is a method in JavaScript that helps extract the substring from a given string.

JavaScript replace()

The string replace() is a built-in JavaScript method that searches a string for a value or a regular expression. The replace() function returns a new string instead of changing the original string.

Syntax

String.replace(specificValue, newValue)

Arguments

searchValue:

It is required, This is a required argument for the given function. This defines the element that needs to be searched for.

requiredValue:

Another required argument for the given function. This is the element with which the searchValue is to be replaced.

Return value:

It returns a string.    //The return value is a string data type, and it is the new string where the given values have been replaced.

Example

let ourWord = "This is Old String";
let ourNewWord = ourWord.replace("Old", "");
console.log(ourNewWord)

Output

This is String

In this example, we want to trim an “Old” substring with some “” empty substring by using the replace() function and assigning the new string to a new string variable. And then, we display the new string using the console.log() method.

Trimming all occurrences of a specific substring

To trim all occurrences of a specific substring in JavaScript, use the regular expression. A regular expression is a pattern of characters. The pattern is used to do pattern-matching “search-and-replace” functions on text. If there are multiple occurrences of a specific substring in the given string, we need to use a Regular expression in place of “specific value”.

let ourWord = "This is Old String. To replace the word Old we need to use RegX and Old will be trimmed.";
let ourNewWord = ourWord.replace(/Old/g, "");
console.log(ourNewWord);

Output

This is String. To replace the word we need to use RegX and will be trimmed.

By using a regular expression, all the occurrences of the specified substring will be replaced by the other as shown in the above code, and we are replacing Old with New value.

Using JavaScript substr() method

The substr() is a built-in JavaScript method that extracts the specific part of a given string. The substr() function takes two parameters: startIndex and length of the extracting string.  You can use the substr() method to trim a substring in JavaScript.

Syntax

String.substr(startIndex, length)

Arguments

start: 

This is a required argument. This defines the start position. Usually, in most programming languages, including javascript, the indexing starts with 0. 

The following two special cases are associated with the argument:

  1. First, if the entered start value is greater than the length of the string, then the given function will return “”, which is an empty string.
  2. If the start is, on the other hand, a negative number, then the counting starts from the end of the string, i.e., reverse.

length:

This parameter defines the number of extracted parts. If this parameter is not passed, it extracts all the parts of the string starting from the start position defined by the user.

Return value

It returns a string containing the extracted part. The function can also return an empty string.

Example

let ourWord = "This is Old String.";
let ourNewWord = ourWord.substr(8, 3);
console.log(ourNewWord)

Output

Old

In this example, we use the substr() function, which is extracting the substring starting from index 8 up to 3 lengths. As an output, we get “Old” displayed on the console using the console.log() function.

That’s it for this tutorial.

See also

How to Convert JavaScript String to Array

How to Convert JavaScript String to Int

How to Convert Date to String in JavaScript

Leave a Comment