Find a line between two lines in javascript or jQuery

I am trying to get a line between two lines in a line. I found many tutorials using regular expressions, but since I am not so good at regex, I cannot figure out how to do this. Any help would 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 line between http://something.com/File/?URL= and and IP = and just go back http://www.wireshock.com . I do not want to split the lines from "&". and get the middle string, because it distorts some URLs with a character and a character in it. Any help would be greatly appreciated. Thanks:)

+6
source share
7 answers
 fullUrl.match(/URL=(.*?)&/i)[1]; 
+12
source
 var matches = fullUrl.match(/URL=(.+)\&IP=/); if (matches.length > 1) { alert(matches[1]); } 

Live demo .

+3
source

Here is the regex you are looking for:

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

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

And a page you can check for future regular expressions on:

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

+3
source

You can use split:

 var result = fullUrl.split('http://something.com/File/?URL=')[1].split('&IP=')[0]; 

Or regex if you really wanted to, but it's pretty fragile. I would recommend you not to do this. Instead, correctly parse the query string as a responsible adult:

How to get query string values ​​in JavaScript?

What if the browser solves other things? Regex or split are torn.

+3
source

There is a fairly simple script that can do this with jQuery (or without), which 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.

Same:

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

This code will give you the exact result you want:

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

This is a simple function to accomplish this in both TypeScript and JavaScript with some exception handling situations:

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

Source: https://habr.com/ru/post/887227/


All Articles