• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Get Substring Between Two Characters

Using JavaScript to Get Substring Between Two Characters

July 11, 2022 Leave a Comment

We can use JavaScript to get the substring between two characters by using the JavaScript String substring(), indexOf(), and lastIndexOf() methods.


var newString = someString.substring(someString.indexOf("FirstCharacter")+1,someString.lastIndexOf("LastCharacter"));

Where someString is the string we want to get the substring from, FirstCharacter is the character we want to start the substring after, and LastCharacter is the character we want our substring to end before.

Let’s see this with a string example:

var someString = "Word1aWord2bWord3";
//We want to get the substring between characters 'a' and 'b'
var substring_between_a_and_b = someString.substring(someString.indexOf('a')+1, someString.lastIndexOf('b'));

console.log(substring_between_a_and_b);

#Output:
Word2

We always like to put our code in a function so we can reuse this multiple times without having to rewrite the code every time. So let’s wrap this code in a simple function:

function getSubstring(string,char1,char2){
  return string.substring(string.indexOf(char1)+1,string.lastIndexOf(char2));  
};

And finally let’s use our string example from above with our function:

function getSubstring(string,char1,char2){
  return string.substring(string.indexOf(char1)+1,string.lastIndexOf(char2));  
};

var someString = "Word1aWord2bWord3";
console.log(getSubstring(someString,'a','b'));

#Output:
Word2

When working with strings in JavaScript, the ability to extract pieces of information from those strings can be valuable.

One such piece of information which can be useful is a substring between two characters.

With JavaScript, we can easily get the characters between two characters using the string substring() method.

First, you need to get the position of each of the two characters. Then we can create a substring to get the characters between the two positions.

Below is our simple example again of how you can get the substring between two characters in JavaScript.

var someString = "Word1aWord2bWord3";
//We want to get the substring between characters 'a' and 'b'
var substring_between_a_and_b = someString.substring(someString.indexOf('a')+1,someString.lastIndexOf('b'));

console.log(substring_between_a_and_b);

#Output:
Word2

Using the slice() Method to Get Substring Between Two Characters

We can also use the JavaScript String slice() method to get the substring between two characters. We simply replace the substring method with the slice method in our example above.

var someString = "Word1aWord2bWord3";
//We want to get the substring between characters 'a' and 'b'
var substring_between_a_and_b = someString.slice(someString.indexOf('a')+1,someString.lastIndexOf('b'));

console.log(substring_between_a_and_b);

#Output:
Word2

Hopefully this article has been useful for you to learn how to use JavaScript to get a substring between two characters.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Add Trailing Zeros
  • 2.  JavaScript sin – Find Sine of Number in Radians Using Math.sin()
  • 3.  Using JavaScript to Convert String to Integer
  • 4.  How to Remove Decimals of a Number in JavaScript
  • 5.  JavaScript atanh – Find Hyperbolic Arctangent of Number
  • 6.  JavaScript Round to Nearest 10
  • 7.  How to Return Multiple Values in JavaScript
  • 8.  JavaScript Capitalize First Letter of Every Word
  • 9.  Remove Parentheses From String Using JavaScript
  • 10.  Using JavaScript to Get Radio Button Value

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy