How To Get A Substring Between 2 Characters In JavaScript

Get a Substring between 2 Characters in JavaScript

There are many methods to help us work with a string in JavaScript. In this article, I will use indexOf() and lastIndexOf() in combination with two substring methods: substring() and substr(), to get a substring between 2 characters in JavaScript.

Get A Substring Between 2 Characters In JavaScript

To continue, we will get acquainted with two methods indexOf() and lastIndexOf(). 

IndexOf() method

Syntax:

string.indexOf(searchvalue, start)

Parameters – Description

  • searchValue: is the string you need to find the address in the string.
  • start: is the position of the character to start the search.

The passed parameter string is searched from left to right and returns the position of the first string to appear, which will return -1 when no string is passed.

LastIndexOf() method

Syntax:

string.lastIndexOf(searchvalue, start)

Parameters – Description

  • searchValue: is the string you need to find the address in the string.
  • start: is the position of the character to start the search.

The passed parameter string searches from right to left and returns the position of the last occurrence of the string that was passed in, which will return -1 if the input string is not found.

Method 1: Use substring() function

The substring() method is a built-in function that takes a substring from the given string. We can combine with indexOf() and lastIndexOf() to get the substring between two characters.

Syntax:

string.substring(start, end)

Parameters – Description

  • start: is the starting character position.
  • end: is the position of the end of the string (not including the character at this position).

Example:

var myString = "Name: Alice;";

var mySubstring = myString.substring(
    myString.indexOf(":") + 1, // 5th
    myString.lastIndexOf(";") // 11th
);

console.log(mySubstring);

Output:

Alice

In the above example, we see that I use the indexOf() method to find the position of the character (:), then add one to get the first position of the string that we want to get, and finally use lastIndexOf() to get it. Output the position of the second character (;), which is also the last value of the string you want to get.

Method 2: Use substr() method

The substr() method is similar to the substring() method, the two methods above differ in that substr() can pass a negative number as a parameter. In the substring() method, if we enter a negative number, the program will understand the parameter by default, which is passed in as 0.

Syntax:

string.substr(start, length)

Parameters – Description

  • start: is the starting character position.
  • length: is the number of characters of the substring you want to split.

Example:

var myString = "Name: Alice;";

var mySubString = myString.substr(
    myString.indexOf(":") + 1, // 5th
    myString.indexOf(";") - (myString.indexOf(":") + 1), // 6
);

console.log(mySubString);

Output:

Alice

Similar to when we use the substring() method, we also use the indexOf() method to find the position of the character (:) to get the first position of the string, then also use lastIndexOf() to get the wallet. The last value of the string you want to get is the same.

Summary

In this article, I have helped you get a substring between 2 characters in JavaScript with the substr(), substring() method. I hope this article will be of help to you. Good luck!

Maybe you are interested:

Leave a Reply

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