0

I want to fetch a string from my website string.
suppose if I have a website https://www.mrkashyap.com/users/manish i want to fetch only the word manish or anything after users/ how can I?
I got the URL from var str= window.location.href I am using this in ReactJS. thanks

2 Answers 2

1

you can do this if you know it will always be the same format:

window.location.href.split('/')

then it'll return an array and access the index in the array you want:

in this case window.location.href.split('/')[4]

Sign up to request clarification or add additional context in comments.

Comments

0

Get location.pathname:

const url = new URL("https://www.mrkashyap.com/users/manish"); // url = location;
console.log(url.pathname.split("/").pop())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.