简体   繁体   English

在 Javascript 或 jQuery 中的两个字符串之间查找字符串

[英]Find string between two strings in Javascript or jQuery

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 .我需要找到一种方法来获取http://something.com/File/?URL=&IP=之间的字符串,然后返回http://www.wireshock.Z4D236D9A2D102C5FE6AD1C50DA4BEC.5 I dont want to split the strings from "&" and get the middle string as it corrupts some urls with the & character in it.我不想从“&”中拆分字符串并获取中间字符串,因为它破坏了一些带有 & 字符的 url。 Any help would be appreciated.任何帮助,将不胜感激。 Thanks:)谢谢:)

fullUrl.match(/URL=(.*?)&/i)[1];
var matches = fullUrl.match(/URL=(.+)\&IP=/);
if (matches.length > 1) {
    alert(matches[1]);   
}

Live demo .现场演示

Here's the regex you're looking for:这是您要查找的正则表达式:

fullUrl.replace(/.*URL=([^&]*)\&.*/,'$1');

http://jsfiddle.net/aL5LU/2/ http://jsfiddle.net/aL5LU/2/

And a page you can test future regexes on:还有一个页面,您可以在以下位置测试未来的正则表达式:

http://www.regular-expressions.info/javascriptexample.html http://www.regular-expressions.info/javascriptexample.html

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? 如何获取 JavaScript 中的查询字符串值?

What if the browser decides to oder things different?如果浏览器决定使用不同的东西怎么办? Regex or split will break.正则表达式或拆分将中断。

This code will give you the exact result as you want:此代码将为您提供所需的确切结果:

var url = fullUrl.split('?URL=')[1].split('/&IP=')[0];

This is simple function to accomplish this in both TypeScript and JavaScript with some exception situation handling:这是简单的 function 在 TypeScript 和 JavaScript 中完成此操作,并进行一些异常情况处理:

TypeScript: 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);
}

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-jFCCquery.ZFC35FDC70D25

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('&');
    //...

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

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