Javascript:在字符串中查找斜杠 (/) 的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9568017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: Find index of slash (/) in a string
提问by Norfeldt
I don't know much about javascript (only done JAVA) but that havent keept me from doing a hack on some javascript code I found (from Google Bookmarks).
我不太了解 javascript(只完成了 JAVA),但这并没有阻止我对我发现的一些 javascript 代码(来自谷歌书签)进行黑客攻击。
Here is my case: I'm creating a bookmark in my browser that contains a javascript instead of a URL link. When I press the bookmark I want it to open a new window with the same url but with some extra info. So say Im on https://www.abcde.com/KnowYourABCand I press the bookmark, then it should open a window with the link: https://www.abcde.com.DoYou/KnowYourABC- so ".DoYou" have been inserted just before the 3th "/"
这是我的情况:我在我的浏览器中创建了一个书签,其中包含一个 javascript 而不是 URL 链接。当我按下书签时,我希望它打开一个具有相同网址但带有一些额外信息的新窗口。所以说我在https://www.abcde.com/KnowYourABC 上,我按下书签,然后它应该打开一个带有链接的窗口:https://www.abcde.com.DoYou/KnowYourABC- 所以“.DoYou”已在第 3 个“/”之前插入
I have the following script:
我有以下脚本:
javascript:(function(){
var a=window, b=document, c=encodeURIComponent,
url = b.location,
d = a.open(url, "bkmk_popup", "left="+
((a.screenX||a.screenLeft)+50)+",top="+
((a.screenY||a.screenTop)+50)+
",height=600px, width=1200px, resizable=1, alwaysRaised=1");
a.setTimeout(function(){d.focus()},300)
})();
So far it opens a window with the same url. But I can't seem to split the url at the 3th "/". Have tried to get the index of the backslash in order to split the url and insert ".DoYou"
到目前为止,它打开了一个具有相同 url 的窗口。但是我似乎无法在第 3 个“/”处拆分 url。尝试获取反斜杠的索引以拆分 url 并插入“.DoYou”
i = url.indexOf("//",9)
but then the window will not open. Please help me out!
但随后窗户就打不开了。请帮帮我!
SOLUTION
解决方案
javascript:( function(){
var a=window;
b=document;
c=encodeURIComponent;
url = b.location;
var parts = window.location.href.split("/");
parts[2] += ".DoYou";
var newurl = parts.join("/");
a.open(newurl, "bkmk_popup","left="+((a.screenX||a.screenLeft)+50)+",top="+((a.screenY||a.screenTop)+50)+",height=600px,width=1200px,resizable=1,alwaysRaised=1"); a.setTimeout(function(){d.focus()},300);
})();
回答by epascarello
It can be done with a simple reg exp, using the parts of the window.location object [host/search/etc] or a simple split and join.
它可以通过一个简单的 reg exp,使用 window.location 对象 [host/search/etc] 的部分或简单的拆分和连接来完成。
Here is the simple split and join solution.
这是简单的拆分和连接解决方案。
var parts = window.location.href.split("/");
parts[2] += ".DOYOU";
var newStr = parts.join("/");
console.log(newStr);
回答by Rocket Hazmat
You don't need to escape the /
(forward slash).
您不需要转义/
(正斜杠)。
i = url.indexOf("/",9)
P.S. The escape character is usually a backslash (\
).
PS 转义字符通常是反斜杠 ( \
)。
回答by ThomasH
In a similar way to the accepted answer, if you don't really need to find the index, but want to place something near it, you can use string.replace(replacedtext, newtext)
and RegEx.
与接受的答案类似,如果您真的不需要找到索引,但想在它附近放置一些东西,您可以使用string.replace(replacedtext, newtext)
和 RegEx。
For your URL example, if you wanted to add text before the first slash with word characters on either side, you could do
对于您的 URL 示例,如果您想在第一个斜杠之前添加文本,两边都有单词字符,您可以这样做
url = url.replace(/(\w(?=\/\w))/, '' + '.DoYou');
The $1
inserts the 1st parenthesized submatch string (see herefor more explanation of the $
).
在$1
插入第一括号的子匹配字符串(见这里为更多的解释$
)。
Example
例子
Like your issue, I wanted to do the same thing except in the same window, so I did:
就像你的问题一样,除了在同一个窗口中,我想做同样的事情,所以我做了:
javascript:void(location.href=location.href.replace(/(\w(?=\/\w))/,''+'.DoYou'))
Having this as a bookmark allows me to just click the bookmark and take me to the page I want.
将此作为书签,我只需单击书签即可将我带到我想要的页面。