11

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

7 Answers 7

20
fullUrl.match(/URL=(.*?)&/i)[1];
Sign up to request clarification or add additional context in comments.

Comments

5

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.

Comments

4

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

Comments

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

Live demo.

Comments

2

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

Comments

1

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

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.