Delete all text up to a character in a URL- JavaScript?


This article will explain how to use the JavaScript string method 'replace()' to delete all text up to a character in a URL. We'll walk through an example of how this technique can be used for cutting off unnecessary information from a URL, and provide some helpful tips on when you might want to use this approach.

A character pattern is what makes up a regular expression. The pattern is applied to text to perform "search-and-replace" operations that match patterns. A RegExp Object in JavaScript is a pattern with properties and methods.

The replace() in JavaScript

The replace() method looks up a value or regular expression in a string. A new string with the replaced value(s) is the result of the replace() method. The original string is unchanged by the replace() technique.

Syntax

Following is the syntax for replace()

string.replace(searchValue, newValue)

Let’s look into the following example, to understand more about how to delete all text up to a character in a url in JavaScript.

Example

In the following example, we use replace() followed by a regular expression to remove all text in a URL up to a character.

<!DOCTYPE html>
<html>
<body>
   <h1>Delete All Text Up To A Character In A Url JavaScript</h1>
   <script>
      var str = "https://www.tutorialspoint.com/index.htm";
      document.write(str.replace(/^.*\//, ''));
   </script>
</body>
</html>

When the script gets executed, it will generate an output consisting of text that was deleted from the url, which was compared with the string along with the regular expressions, and remove the text and display it on the web-browser.

Example

Considering another example, where we are running the script to check if the part with "http" was found or not, If it is found, it will remove the whole thing that comes after http.

<!DOCTYPE html>
<html>
<body>
   <script>
      var string = "Welcome https://www.tutorialspoint.com/index.htm";
      string = string.split(" ");
      for (var i = 0; i < string.length; i++) {
         if (string[i].substring(0, 4) === "http") {
            string.splice(i, 1);
         }
      }
      document.write(string.join(" "));
   </script>
</body>
</html>

On running the above script, the web-browser triggers the event, and compare the given string with the regular expression based upon the provided condition. It will then display the text that was before http and delete the whole content that was with http.

Example

Let’s look at another example, where we are using regular expressions to remove all the text-up characters in the url and display the remaining content.

<!DOCTYPE html>
<html>
<body>
   <script>
      var url ="https://www.tutorialspoint.com/advanced_microsoft_word_2013_tutorial/index.asp";
      var getValues=url.replace(/^.*\//, '');
      document.write("The original value = "+url+"<br>");
      document.write("New Value = "+getValues);
   </script>
</body>
</html>

When the script is executed, it generates an output, triggering the event and displaying the original url and updated value on the webpage, as the regular expression replaced the url text up.

Updated on: 18-Jan-2023

720 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements