繁体   English   中英

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

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

我一直试图在一行中的两个字符串之间获取一个字符串。 我发现了很多使用正则表达式的教程,但由于我不擅长正则表达式,所以我无法弄清楚如何去做。 任何帮助将不胜感激。

var fullUrl = "http://something.com/File/?URL=http://www.wireshock.com/&IP=0.0.0.0&CAT=BLOG&USER=MAND\\DEFAULT\\market4080";

我需要找到一种方法来获取http://something.com/File/?URL=&IP=之间的字符串,然后返回http://www.wireshock.Z4D236D9A2D102C5FE6AD1C50DA4BEC.5 我不想从“&”中拆分字符串并获取中间字符串,因为它破坏了一些带有 & 字符的 url。 任何帮助,将不胜感激。 谢谢:)

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

现场演示

这是您要查找的正则表达式:

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

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

还有一个页面,您可以在以下位置测试未来的正则表达式:

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

您可以使用拆分:

var result = fullUrl.split('http://something.com/File/?URL=')[1].split('&IP=')[0];

或者如果你真的想要一个正则表达式,但这很脆弱。 我建议你不要这样做。 相反,像负责任的成年人一样正确解析查询字符串:

如何获取 JavaScript 中的查询字符串值?

如果浏览器决定使用不同的东西怎么办? 正则表达式或拆分将中断。

此代码将为您提供所需的确切结果:

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

这是简单的 function 在 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:

/**
 * 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);
}

有一个相当简单的脚本可以使用 jQuery(或不带)执行此操作,可以在此处找到http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jFCCquery.ZFC35FDC70D25

只需将window.location.href替换为参数即可。

像这样:

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