- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get substring between two similar characters in JavaScript?
Before we are going to learn about how to get substring between two similar characters in JavaScript. Let’s have a quick view on the characters in JavaScript.
In a string, the charAt() method returns the character that is located at the given index (position). First character's index is 0, second character's index is 1.
Getting substring in JavaScript
The substring() method takes a string and extracts all characters between two indices (positions), then it returns the substring. Characters are taken out of a string using the substring() technique. The initial string is left alone when using the substring() technique.
Syntax
Following is the syntax for substring() −
string.substring(start, end)
Let’s dive into the article to learn more about getting substring between two similar characters in JavaScript. For this, we can use the concept of split().
Using split() in JavaScript
Splitting a string into an array of substrings is done using the split() method. split() produces a new array as its output. The string's original content is unaltered by the split() technique. When the separator ("") is applied, the string is divided into words.
Syntax
Following is the syntax for split() −
string.split(separator, limit)
Example
n the following example, we are running a script and passing a value; later, we have declared var=result[] to get the output using split().
<!DOCTYPE html> <html> <body> <script> var result = []; const str1 = 'one:two;three'; result = str1.split(":"); document.write(result[0]); </script> </body> </html>
When the script gets executed, it will generate an output consisting of the value "one," which will be displayed on the webpage as the event gets triggered by running the script.
Example
Let’s look into the following example, where we are using split() for getting the desired output.
<!DOCTYPE html> <html> <body> <script> var s = 'HelloIm cat:Your arewelcome to tutorialspoint dog;'; var arrayStr = s.split(/[:;]/); alert(arrayStr[1]); </script> </body> </html>
When the script gets run, the event gets triggered and displays the text of the index we have used in the script in the alertbox.
Example
Consider the following example, here we are using the split() method, which divides the string into substrings, and the substrings are placed in an array. Later, we will pass an index value to get the desired output.
<!DOCTYPE html> <html> <body> <script> let str = 'xyz:welcome to tutorialspoint:66df88v:'.split(':') document.write(str[1]) </script> </body> </html>
On running the above script, the output window will pop up, displaying a text on the webpage that was caused by the event that got triggered on running the script.
Example
In the following example, we are running the script along with slice() and mentioning the indices to get the desired output on the screen page.
<!DOCTYPE html> <html> <body> <script> let str = 'tp:The Best E-Way Learning:007jb700:' document.write(str.slice(3, 35)) </script> </body> </html>
When the script gets executed, it will generate an output consisting of a text that was lying between (3, 35), (start value), and (end value) that is printed on the webpage obtained by an event that gets triggered on running the script.