简体   繁体   English

如何使用纯javascript获取URL字符串的一部分

[英]How to get a part of a url string with pure javascript

I have this url, for example: 我有这个网址,例如:

https://www.google.com/calendar/render#main_7%7Cmonth

I need to get ONLY calendar with or witout / 我只需要带或不带calendar /

Why in pure JavaScript? 为什么使用纯JavaScript? I want to put it in a bookmark, so i cant use any kind of JS.Framework... 我想把它放在书签中,所以我不能使用任何类型的JS.Framework ...

Some of you could say that post is duplicated. 你们中有些人可能会说帖子重复了。 Thats why you dont use the search-tool nor read the entire question. 这就是为什么您不使用搜索工具也不阅读整个问题的原因。 Thanks for other who really read everything. 感谢其他真正阅读所有内容的人。

PD:Yeah, i've readed this solution.. this does not fit me. PD:是的,我已经阅读了此解决方案..这不适合我。 JavaScript - Get Portion of URL Path JavaScript-获取部分URL路径

Assuming calendar is always on the same position you could do 假设calendar始终位于您可以执行的位置

var url = 'https://www.google.com/calendar/render#main_7%7Cmonth';
var calendar = url.split('/')[3];

使用split函数将字符串划分为数组:

"https://www.google.com/calendar/render#main_7%7Cmonth".split('/')[3];

(回答这个问题我有点伤心)。

var whatyouwant = url.split('/')[3];

var url = "https://www.google.com/calendar/render#main_7%7Cmonth";
var split_url = url.split('/');
for(var i=0; i < split_url.length; i++)
  if(split_url[i] == 'calendar')
    alert("It's a calendar");

Search for calendar and extract the remainder of the string - 搜索日历并提取字符串的其余部分-

var s = 'https://www.google.com/calendar/render#main_7%7Cmonth';
var o = s.indexOf('calendar');
if (o !== -1) {
    // Found calendar at offset o
    console.log(o);
    console.log(s.substr(o));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM