Computer >> Computer tutorials >  >> Programming >> Javascript

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


Let’s say the following is our URL −

https://www.tutorialspoint.com/advanced_microsoft_word_2013_tutorial/index.asp";

We only need to get the last value and delete the rest i.e. the output should be −

index.asp

For this, you can use replace() along with regular expression.

Example

Following is the code −

var url = "https://www.tutorialspoint.com/advanced_microsoft_word_2013_tutorial/index.asp";
var getValues=url.replace(/^.*\//, '');
console.log("The original value = "+url);
console.log("New Value = "+getValues);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo258.js.

Output

This will produce the following output on console −

PS C:\Users\Amit\javascript-code> node demo258.js
The original value = https://www.tutorialspoint.com/advanced_microsoft_word_2013_tutorial/index.asp
New Value = index.asp