How to get value of a string after last slash in JavaScript? Last Updated : 31 May, 2024 Comments Improve Suggest changes Like Article Like Report The task is to get the string after a specific character('/'). Here are a few of the most used techniques discussed. We are going to use JavaScript. Below are the approaches used to get the value of a string after the last slash in JavaScript: Table of Content Approach 1: Using .split() method and .length propertyApproach 2: Using .lastIndexOf(str) method and .substring() methodApproach 3: Using .split() method and pop() methodApproach 4: Using Regular ExpressionsApproach 1: Using .split() method and .length propertySplit the string by .split() method and put it in a variable(array).Use the .length property to get the length of the array.From the array return the element at index = length-1.Example: This example uses the approach discussed above. JavaScript let str = "folder_1/folder_2/file.html"; function GFG_FUN() { str = str.split("/"); console.log(str[str.length - 1]); } GFG_FUN(); Outputfile.html Approach 2: Using .lastIndexOf(str) method and .substring() methodFirst, find the last index of ('/') using .lastIndexOf(str) method.Use the .substring() method to get access the string after the last slash.Example: This example uses the approach discussed above. JavaScript let str = "folder_1/folder_2/file.html"; function GFG_FUN() { console.log(str.substring(str.lastIndexOf('/') + 1)); } GFG_FUN(); Outputfile.html Approach 3: Using .split() method and pop() methodSplit the string by .split() method pop the variable by using pop() methodExample: This example uses the above approach. JavaScript let str = "folder_1/folder_2/file.html"; function GFG_FUN() { str = str.split("/").pop(); console.log(str); } GFG_FUN(); Outputfile.html Approach 4: Using Regular ExpressionsRegular expressions provide a powerful way to search for patterns within strings. We can use a regular expression to match the portion of the string after the last slash. Example: JavaScript let str = "folder_1/folder_2/file.html"; function getStringAfterLastSlash(str) { // Match any characters after the last slash const regex = /[^/]+$/; // Extract the matched portion using regex const match = str.match(regex); // Return the matched portion return match ? match[0] : ""; } console.log(getStringAfterLastSlash(str)); Outputfile.html Comment More infoAdvertise with us Next Article How to get value of a string after last slash in JavaScript? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads JavaScript - How To Get The Last Caracter of a String? Here are the various approaches to get the last character of a String using JavaScript.1. Using charAt() Method (Most Common)The charAt() method retrieves the character at a specified index in a string. To get the last character, you pass the index str.length - 1.JavaScriptconst s = "JavaScript"; co 3 min read JavaScript - How to Globally Replace a Forward Slash in a String? Here are the various methods to globally replace a forward slash in the JavaScript string.1. Using replace() Method with a Regular ExpressionThe replace() method is commonly used with a regular expression to replace all occurrences of a forward slash.JavaScriptconst s1 = "path/to/some/resource"; con 2 min read How to replace a portion of strings with another value in JavaScript ? In JavaScript, replacing a portion of strings with another value involves using the `replace()` method or regular expressions. This process is essential for text manipulation tasks like formatting, sanitization, or dynamic content generation in web development and data processing applications. We ca 3 min read How to get the last character of a string in PHP ? In this article, we will find the last character of a string in PHP. The last character can be found using the following methods.Using array() Method: In this method, we will find the length of the string, then print the value of (length-1). For example, if the string is "Akshit" Its length is 6, in 2 min read How to get nth occurrence of a string in JavaScript ? In this article, the task is to get the nth occurrence of a substring in a string with the help of JavaScript. We have many methods to do this some of which are described below:Approaches to get the nth occurrence of a string:Table of Content Using split() and join() methodsUsing indexOf() methodUsi 5 min read Like