How to useRegular Expressions in Javascript
Regular 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:
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));
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