
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to remove two parts of a string with JavaScript?
This tutorial teaches us how to remove the text part between two parts of a string with JavaScript, basically, we are given two ends that can be a string or a character and we have to remove the string lying in between them.
In this tutorial, we are going to use the regular expression or JavaScript Regex to remove the text between two characters. We are going to use the replace() function of JavaScript.
Syntax
Let’s see the syntax of removing part of a string in between two characters or sub-strings of a given string using JavaScript Regex −
var str; // declaration of the string var final_str = str.replace( / (first_part) .* (second_part)/ , (first_part) + (second_part ));
In the above syntax, we first declared a string in which we are going to make changes and final_str will store the string which will be our final result. We are going to use the replace() method of JavaScript regex in which we are pass regular expression − ‘/ (first_part) .* (second_part)/’. Here first_part can be a string or a character.
Note − Here we are trying to find the first occurrence of the ‘first_part’ in the given string, then we will find the last occurrence of the ‘second_part’ in the given string. Then if both the ‘first_part’ and the ‘second_part’ are present in the string and there are some characters present in between them, we will remove them.
Algorithm
We have seen the syntax above to remove part of a string between two characters, now we are going to see the complete algorithm step by step −
First, we will declare the string and assign it some value.
Then we will create a variable that will store the value of the final result.
We will call the JavaScript regex method to replace() and pass it the regular expression, but here is a problem that is replace() function needs two parameters.
We will find the regular expression which will include our both starting and ending strings or characters which means they are also going to replace with the middle string which we actually need to return.
We will pass our first and last strings or character as the combined strings to replace the given regular expression.
In this way, we can remove the string between the two parts of the string.
Now, let’s see the examples which are based on the above algorithm −
Example 1
In this example we will take two strings and will delete the middle string which will be present in between them −
<!DOCTYPE html> <html> <body> <script> var str = "ABCDEFGHIJABCDEFGHIJABCDEFGHIJ"; // declaration of the string var final_str = str.replace( /CD.*GH/ , 'CDGH'); document.write("Previous string was: " + str); document.write("<br>Final stirng is: " + final_str); </script> </body> </html>
In the above example, we have removed all the characters present between the ‘CD’ and ‘GH’. Here we replaced string from index 2 (0-base index) to index 27 and then replace them with ‘CDGH’ to get the final answer.
Example 2
In this example, we are going to replace all the characters in between two characters and we will remove these characters also.
<!DOCTYPE html> <html> <body> <script> var str = "AB(CDEFGHIJABCDEFGHIJABCDEFGH)IJ"; // declaration of the string var final_str = str.replace(/ *\([^)]*\) */g, ""); document.write("Previous string was: " + str); document.write("<br>Final reuslt is: " + final_str) </script> </body> </html>
In the above example, we have removed all the characters present between the parenthesis including the parenthesis.
Conclusion
In this tutorial we have learned how to remove the text part between two parts of a string, basically, we are given two ends that can be a string or a character and we learned to remove the string lying in between them. We used the regular expression or JavaScript Regex to remove the text between two characters by using replace() function.
- Related Articles
- How to remove “,” from a string in JavaScript
- Remove characters from a string contained in another string with JavaScript?
- Replace parts of a string with C# Regex
- Splitting a string into parts in JavaScript
- How to remove html tags from a string in JavaScript?
- Divide a string into n equal parts - JavaScript
- Splitting a string into maximum parts in JavaScript
- Split string into equal parts JavaScript
- How to add two strings with a space in first string in JavaScript?
- Program to check two parts of a string are palindrome or not in Python
- How to remove options from a dropdown list with JavaScript?
- How to return a new string with a specified number of copies of an existing string with JavaScript?
- How to remove a class name from an element with JavaScript?
- How to remove HTML Tags with RegExp in JavaScript?
- Remove extra spaces in string JavaScript?
