开发者

Find string between two strings in Javascript or jQuery

开发者 https://www.devze.com 2023-03-03 03:22 出处:网络
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 w

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 开发者_JAVA百科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 :)


fullUrl.match(/URL=(.*?)&/i)[1];


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.


Here's the regex you're looking for:

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

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

And a page you can test future regexes on:

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


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

Live demo.


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:

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


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

Just replace window.location.href with an argument.

Like so:

function getUrlVars(url)
{
    var vars = [], hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
    //...
0

精彩评论

暂无评论...
验证码 换一张
取 消