Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To get string after last slash in javascript, use split() method with pop() method it will return those string which after then your last slash or which you used to split the string.

Let’s see short example to use split() method with pop() method to get string after last slash.

string.split("/").pop();

Today, I’m going to show you How do I get string after last slash in javascript, as above mentioned, I’m going to use the above-mentioned split() method with pop() method.

Let’s start today’s tutorial how do you get string after last slash in javascript?

Here, I will show get string after last slash examples in javascript, typescript, react js and react native.

Javascript get string after last slash example

Here, we will take string variable with some data and use split() with pop() method to get string after last slash in javascript.

let string = "file/newuser.jpg";

let afterLifeStr = string.split("/").pop();

console.log(afterLifeStr)

// Output
// 'newuser.jpg'

Output

Typescript get string after last slash example

Here, we will take string variable with some data and use split() with pop() method to get string after last slash in typescript.

let string: string = "file/newuser.jpg";

let afterLifeStr: string = string.split("/").pop();

console.log(afterLifeStr)

// Output
// 'newuser.jpg'

Output

React js get string after last slash example

Here, we will take string state with some data and use split() with pop() method to get string after last slash in react js.

import React, { useState, useEffect } from "react";
export default function App() {
  const [str, setStr] = useState("file/newuser.jpg");

  useEffect(() => {
    let afterLifeStr = str.split("/").pop();
    console.log(afterLifeStr)
  }, []);

  return (
    <div className="App">
      {str.split("/").pop()}
    </div>
  );
}

Output

React native get string after last slash example

Here, we will take string state with some data and use split() with pop() method to get string after last slash in react native.

import React, { useState, useEffect } from "react";
import { Text, View } from 'react-native';

export default function App() {
  const [str, setStr] = useState("file/newuser.jpg");

  useEffect(() => {
    let afterLifeStr = str.split("/").pop();
    console.log(afterLifeStr)
  }, []);

  return (
    <View>
      <Text>{str.split("/").pop()}</Text>
    </View>
  );
}

Output

I hope it helps you, All the best đź‘Ť.