Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to get value of a string after last slash in JavaScript?
Next article icon

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 property
  • Approach 2: Using .lastIndexOf(str) method and .substring() method
  • Approach 3: Using .split() method and pop() method
  • Approach 4: Using Regular Expressions

Approach 1: Using .split() method and .length property

  • Split 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(); 

Output
file.html 

Approach 2: Using .lastIndexOf(str) method and .substring() method

  • 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. 

JavaScript
let str = "folder_1/folder_2/file.html";  function GFG_FUN() {     console.log(str.substring(str.lastIndexOf('/') + 1)); }  GFG_FUN(); 

Output
file.html 

Approach 3: Using .split() method and pop() method

  • Split the string by .split() method 
  • pop the variable by using pop() method

Example: 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(); 

Output
file.html 

Approach 4: Using Regular Expressions

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:

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)); 

Output
file.html 



Next Article
How to get value of a string after last slash in JavaScript?

P

PranchalKatiyar
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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences