Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 879
KSubedi Avatar asked May 03 '11 19:05

KSubedi


People also ask

How to get substring between two strings in jQuery?

Using split() method: It simply split a string into an array of substrings by a specified character or string and returns the new array.

How to get string between two words in JavaScript?

Answer: Use the JavaScript match() method You can use the JavaScript match() method to extract substring between two words.

How to extract substring from string in JavaScript?

The substr() method extracts a part of a string. The substr() method begins at a specified position, and returns a specified number of characters. The substr() method does not change the original string. To extract characters from the end of the string, use a negative start position.

How do you get a substring from a string in TypeScript?

TypeScript | String substr() MethodThe split() is an inbuilt function in TypeScript which is used to returns the characters in a string beginning at the specified location through the specified number of characters. start – This parameter is the location at which to start extracting characters.


2 Answers

fullUrl.match(/URL=(.*?)&/i)[1];
like image 95
mVChr Avatar answered Sep 20 '22 17:09

mVChr


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.

like image 32
Alex Wayne Avatar answered Sep 20 '22 17:09

Alex Wayne