简体   繁体   中英

Remove name from url query using regular expression

How can I remove the " /page/2/ " from the url using a regular expression in javascript. Where the 2 can be any number ie. /page/ 5 /. but "page" and the slashes will be consistently there.

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/

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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