How to use.lastIndexOf(str) method and .substring() method in Javascript
- First, 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.
let str = "folder_1/folder_2/file.html";
function GFG_FUN() {
console.log(str.substring(str.lastIndexOf('/') + 1));
}
GFG_FUN();
Output
file.html
How to get value of a string after last slash in JavaScript?
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 property
- Approach 2: Using .lastIndexOf(str) method and .substring() method
- Approach 3: Using .split() method and pop() method
- Approach 4: Using Regular Expressions
Contact Us