J'ai essayé d'obtenir une ficelle entre deux cordes dans une ligne. J'ai trouvé beaucoup de tutoriels à l'aide de Regex mais comme je ne suis pas si bon chez Regex, je ne suis pas capable de comprendre comment le faire. Toute aide sera appréciée.
var fullUrl = "http://something.com/File/?URL=http://www.wireshock.com/&IP=0.0.0.0&CAT=BLOG&USER=MAND\\DEFAULT\\market4080";
7 Réponses :
Il existe un script assez facile qui peut faire cela avec jQuery (ou sans) que vous pouvez trouver ici http: //jquery-howto.blogspot .COM / 2009/09 / GET-URL-paramètres-valeurs-with-jquery.html
Il suffit de remplacer fenêtre.location.href code> avec un argument. P> < P> Comme: p>
var matches = fullUrl.match(/URL=(.+)\&IP=/);
if (matches.length > 1) {
alert(matches[1]);
}
Live demo.
Voici la regex que vous recherchez: http: / /jsfiddle.net/al5lu/2/ p> et une page Vous pouvez tester les futures expressions sur: p> http://www.regular-expressions.info/javascriptexample.html p> p> p>
Vous pouvez utiliser Split: ou une regex si vous vouliez vraiment, mais c'est assez fragile cependant. Je vous recommanderais de ne pas faire ça. Au lieu de cela, analyser correctement la chaîne de requête comme un adulte responsable: p> Comment puis-je Obtenir des valeurs de la chaîne de requête dans JavaScript? P> Et si le navigateur décide d'Oder Things Différent? Regex ou Split se briseront. P> P>
fullUrl.match(/URL=(.*?)&/i)[1];
Ce code vous donnera le résultat exact que vous le souhaitez:
var url = fullUrl.split('?URL=')[1].split('/&IP=')[0];
Ceci est une fonction simple pour accomplir ceci dans les documents dactylographique et JavaScript avec une manipulation de la situation d'exception:
Typecript: p> JavaScript: p> /**
* 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);
}