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.

Updated on: 07-Nov-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements