2015-09-04 1 views
0

Existe-t-il un moyen de remplacer tous les caractères avant le point d'interrogation dans une URL via un bookmarklet javascript?Remplacer les caractères avant le point d'interrogation de l'URL actuelle avec javascript bookmarklet

J'ai trouvé un moyen de remplacer des parties dans l'URL, mais seulement si l'URL actuelle est la même tout le temps.

Mais mon URL est différent avec beaucoup de sous-pages, alors je dois tout isoler avant que la question Mark

Exemple 1:

http://www.marktplaats.nl/z.html requête = Bucky & searchOnTitleAndDescription = true & categoryId = 356 & code postal =

Doit être changé pour:

http://www.rss2search.nl/marktplaats/zoek.php? Query = Bucky & searchOnTitleAndDescription = true & categoryId = 356 & code postal =

Puis-je modifier en sélectionnant tout avant le point d'interrogation?

+0

Bienvenue sur StackOverflow, s'il vous plaît jeter un oeil à [cette page] (http: //stackoverflow.com/help/how-to-ask) pour voir comment vous pourriez améliorer votre question. Par exemple: essayez toujours de fournir un exemple de code de ce que vous avez déjà essayé. – Jordumus

Répondre

0

Convertissez votre URL dans un tableau:

var arr = 'http://www.marktplaats.nl/z.html?query=buck...'.split('?'); 

Rejoignez le texte URL et le deuxième élément de arr ensemble:

var text = 'http://www.rss2search.nl/marktplaats/zoek.php'; 
var out = [text, arr[1]].join('?'); 

DEMO

0

Oui, il peut être fait comme ce ...

var url = "http://www.rss2search.nl/marktplaats/zoek.php"; 
location.href = url + location.href.substr(location.href.indexOf("?")); 

Pour votre bookmarklet, ajoutez l'URL ...

javascript:(function(){location.href="http://www.rss2search.nl/marktplaats/zoek.php"+location.href.substr(location.href.indexOf("?"));return false;})() 
0

Vous pouvez ci-dessous le code:

// This regex pattern will take whatever string before question mark 
var re = /(.+)\?/; 

var str = 'http://www.marktplaats.nl/z.html?query=bucky&searchOnTitleAndDescription=true&categoryId=356&postcode='; 

// And then replace with this string 
var subst = 'http://www.rss2search.nl/marktplaats/zoek.php'; 

var result = str.replace(re, subst);