简体   繁体   中英

Remove Preceding Characters in RegExp Javascript

I have a problem sanitizing dynamic URLs eg

 var uri = '/localhost:8080////app//user/login.json?foo=bar';
 var sanitizedUri = uri.replace(/\.json*/, '');

 // print ==> /localhost:8080////app//user/login?foo=bar

What I want here is when " .json " is present in the URL, I want to remove .json and succeeding characters (query params) and will only output the following:

/localhost:8080////app//user/login<br>

Note foo=bar is a dynamic value.

Try adding .

(The dot, the decimal point) matches any single character except line

to RegExp before * to

Matches the preceding item x 0 or more times.

following \\.json

 var uri = '/localhost:8080////app//user/login.json?foo=bar'; var sanitizedUri = uri.replace(/\\.json.*/, ''); console.log(sanitizedUri) 

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