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 :)
Using split() method: It simply split a string into an array of substrings by a specified character or string and returns the new array.
Answer: Use the JavaScript match() method You can use the JavaScript match() method to extract substring between two words.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With