[英]how to fetch a string from a string in JS/ReactJs
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?假设如果我有一个网站
https://www.mrkashyap.com/users/manish
我只想获取单词manish
或用户之后的任何内容/我该怎么办?
I got the URL from var str= window.location.href
I am using this in ReactJS.我从
var str= window.location.href
得到了 URL 我在 ReactJS 中使用它。 thanks谢谢
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]
在这种情况下
window.location.href.split('/')[4]
Get location.pathname:获取位置.路径名:
const url = new URL("https://www.mrkashyap.com/users/manish"); // url = location; console.log(url.pathname.split("/").pop())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.