J'ai une URL:
http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe
Je veux obtenir l'adresse après le dernier tiret en utilisant javascript:
dashboard.php?page_id=projeto_lista&lista_tipo=equipe
7 réponses
Si le début est toujours "http: // localhost / 40ATV", vous pouvez le faire:
var a = "http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe";
var cut = a.substr(22);
La méthode native de chaîne JavaScript substr
[ MDN] peut accomplir ce dont vous avez besoin. Fournissez simplement l'index de départ et omettez le paramètre de longueur, et il s'accroche jusqu'à la fin.
Maintenant, comment obtenir l'index de départ? Vous n'avez donné aucun critère, je ne peux donc pas vraiment vous aider.
Cela peut être nouveau, mais la substring renvoie tout depuis un index spécifié jusqu'à la fin de la chaîne.
var string = "This is a test";
console.log(string.substring(5));
// returns "is a test"
Tout d'abord SPLIT URL:
var str = "http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe";
var arr_split = str.split("/");
Trouvez le dernier tableau:
var num = arr_split.length-1;
Vous obtenez l'adresse après le dernier tiret:
alert(arr_split[num]);
Vous pouvez utiliser indexOf
et substr
pour obtenir la sous-chaîne souhaitée:
//using a string variable set to the URL you want to pull info from
//this could be set to `window.location.href` instead to get the current URL
var strIn = 'http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe',
//get the index of the start of the part of the URL we want to keep
index = strIn.indexOf('/dashboard.php'),
//then get everything after the found index
strOut = strIn.substr(index);
La variable strOut
contient désormais tout ce qui se trouve après /dashboard.php
(y compris cette chaîne).
Voici une démo: http://jsfiddle.net/DupwQ/
MISE À JOUR:
La variable strOut
dans l'exemple ci-dessus inclut la barre oblique préfixée et il a été demandé que la sortie ne le fasse pas.
Le remplacement de strOut = strIn.substr(index)
par strOut = strIn.substr(index + 1)
corrige la sortie de ce cas d'utilisation spécifique en commençant la sous-chaîne un caractère plus loin dans la chaîne.
Vous pouvez également rechercher la chaîne après un terme de recherche spécifique (non inclus):
var strIn = 'http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe';
var searchTerm = '/dashboard.php?';
var searchIndex = strIn.indexOf(searchTerm);
var strOut = strIn.substr(searchIndex + searchTerm.length); //this is where the magic happens :)
strOut
contient désormais tout ce qui se trouve après /dashboard.php?
(non inclus).
Voici une démo mise à jour: http://jsfiddle.net/7ud0pnmr/1/
Documents -
indexOf()
: https://developer.mozilla.org/ fr / JavaScript / Reference / Global_Objects / String / indexOfsubstr()
: https://developer.mozilla.org/ fr / JavaScript / Reference / Global_Objects / String / substrString.length
: https: // developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length
Pas besoin de jQuery, le vieux javascript simple fera très bien l'affaire.
var myString = "http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe";
var mySplitResult = myString.split("\/");
document.write(mySplitResult[mySplitResult.length - 1]);
Et si vous voulez le leader /
document.write("/" + mySplitResult[mySplitResult.length - 1]);
Vous pouvez utiliser ce code, s'il n'y a pas dashboard...
de retour vide.
var str = 'http://localhost/40ATV/dashboard.php?page_id=projeto_lista&lista_tipo=equipe';
var index = str.indexOf('/dashboard.php') + 1;
var result = '';
if (index != 0) {
result = str.substr(index);
}
console.log(result);
De nouvelles questions
javascript
Pour des questions concernant la programmation dans ECMAScript (JavaScript / JS) et ses divers dialectes / implémentations (hors ActionScript). Veuillez inclure toutes les balises pertinentes dans votre question; par exemple, [node.js], [jquery], [json], etc.