在 Javascript 或 jQuery 中的两个字符串之间查找字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5874670/
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
Find string between two strings in Javascript or jQuery
提问by KSubedi
I have been trying to get a string between two strings in a line. I found a lots of tutorials using regex but as i am not that good at regex, i am not being able to figure out how to do it. Any help will be appreciated.
我一直试图在一行中的两个字符串之间获取一个字符串。我发现了很多使用正则表达式的教程,但由于我不太擅长正则表达式,我无法弄清楚如何去做。任何帮助将不胜感激。
var fullUrl = "http://something.com/File/?URL=http://www.wireshock.com/&IP=0.0.0.0&CAT=BLOG&USER=MAND\DEFAULT\market4080";
i need to figure out a way to get the string between http://something.com/File/?URL=and &IP=and just return http://www.wireshock.com. I dont want to split the strings from "&" and get the middle string as it corrupts some urls with the & character in it. Any help would be appreciated. Thanks :)
我需要找到一种方法来获取http://something.com/File/?URL=和&IP=之间的字符串,然后返回http://www.wireshock.com。我不想从“&”中拆分字符串并获取中间字符串,因为它会破坏一些带有 & 字符的网址。任何帮助,将不胜感激。谢谢 :)
回答by mVChr
fullUrl.match(/URL=(.*?)&/i)[1];
回答by Milimetric
Here's the regex you're looking for:
这是您正在寻找的正则表达式:
fullUrl.replace(/.*URL=([^&]*)\&.*/,'');
And a page you can test future regexes on:
还有一个页面,您可以测试未来的正则表达式:
回答by Alex Wayne
You could use split:
您可以使用拆分:
var result = fullUrl.split('http://something.com/File/?URL=')[1].split('&IP=')[0];
Or a regex if you really wanted, but this is pretty brittle though. I would recommend you not do this. Instead, parse the query string properly like a responsible adult:
或者如果你真的想要一个正则表达式,但这很脆弱。我建议你不要这样做。相反,像负责任的成年人一样正确解析查询字符串:
How can I get query string values in JavaScript?
What if the browser decides to oder things different? Regex or split will break.
如果浏览器决定使用不同的东西怎么办?正则表达式或拆分将中断。
回答by Darin Dimitrov
回答by Vishaal Kathiriya
This code will give you the exact result as you want:
此代码将为您提供所需的确切结果:
var url = fullUrl.split('?URL=')[1].split('/&IP=')[0];
回答by Tommi Laukkanen
This is simple function to accomplish this in both TypeScript and JavaScript with some exception situation handling:
这是在 TypeScript 和 JavaScript 中完成此操作的简单函数,并具有一些异常情况处理:
TypeScript:
打字稿:
/**
* Parses substring between given begin string and end string.
* @param beginString the begin string
* @param endString the end string
* @param originalString the original string
* @returns the substring or null if either tag is not found
*/
export function parseBetween(beginString, endString, originalString): string {
var beginIndex: number = originalString.indexOf(beginString);
if (beginIndex === -1) {
return null;
}
var beginStringLength: number = beginString.length;
var substringBeginIndex: number = beginIndex + beginStringLength;
var substringEndIndex: number = originalString.indexOf(endString, substringBeginIndex);
if (substringEndIndex === -1) {
return null;
}
return originalString.substring(substringBeginIndex, substringEndIndex);
}
JavaScript:
JavaScript:
/**
* Parses substring between given begin string and end string.
* @param beginString the begin string
* @param endString the end string
* @param originalString the original string
* @returns the substring or null if either tag is not found
*/
function parseBetween(beginString, endString, originalString) {
var beginIndex = originalString.indexOf(beginString);
if (beginIndex === -1) {
return null;
}
var beginStringLength = beginString.length;
var substringBeginIndex = beginIndex + beginStringLength;
var substringEndIndex = originalString.indexOf(endString, substringBeginIndex);
if (substringEndIndex === -1) {
return null;
}
return originalString.substring(substringBeginIndex, substringEndIndex);
}
回答by John Giotta
There is a fairly easy script that can do this with jQuery (or without) that can be found here http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
有一个相当简单的脚本可以使用 jQuery(或不使用)来做到这一点,可以在这里找到 http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
Just replace window.location.href
with an argument.
只需window.location.href
用参数替换即可。
Like so:
像这样:
function getUrlVars(url)
{
var vars = [], hash;
var hashes = url.slice(url.indexOf('?') + 1).split('&');
//...