KSubedi
KSubedi

Reputation: 463

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. 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 :)

Upvotes: 11

Views: 41343

Answers (7)

Tommi Laukkanen
Tommi Laukkanen

Reputation: 21

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

Upvotes: 2

Vishaal Kathiriya
Vishaal Kathiriya

Reputation: 21

This code will give you the exact result as you want:

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

Upvotes: 1

Milimetric
Milimetric

Reputation: 13549

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

Upvotes: 4

mVChr
mVChr

Reputation: 50177

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

Upvotes: 20

Alex Wayne
Alex Wayne

Reputation: 187004

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.

Upvotes: 5

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

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

Live demo.

Upvotes: 3

John Giotta
John Giotta

Reputation: 16934

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

Upvotes: 0

Related Questions