简体   繁体   English

使用正则表达式从 url 查询中删除名称

[英]Remove name from url query using regular expression

How can I remove the " /page/2/ " from the url using a regular expression in javascript.如何使用javascript中的正则表达式从url中删除“ /page/2/ ”。 Where the 2 can be any number ie.其中 2 可以是任何数字,即。 /page/ 5 /. /页/ 5 /。 but "page" and the slashes will be consistently there.但是“页面”和斜杠将始终存在。

mydomain.com/posts/page/2/ mydomain.com/posts/page/2/

Use this pattern : (.*)[^\\/]*\\/[^\\/]*\\/[^\\/]*\\/$使用这种模式: (.*)[^\\/]*\\/[^\\/]*\\/[^\\/]*\\/$

Full code :完整代码:

var string = 'mydomain.com/posts/page/2/';
var result = /(.*)[^\/]*\/[^\/]*\/[^\/]*\/$/.exec(string);
var wantedPath = result[1];

Testable here : http://jsfiddle.net/3h7dX/可在这里测试: http : //jsfiddle.net/3h7dX/

note : page can be other thing as your number value.注意:页面可以是其他东西作为您的数字值。

Please, valid the answer to close your question if it is correct.如果答案正确,请验证答案以结束您的问题。

string = string.replace(/\/page\/\d+\//, "");

This should solve your problem.这应该可以解决您的问题。 Example is under http://jsfiddle.net/AfR4D/示例在http://jsfiddle.net/AfR4D/ 下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM