简体   繁体   中英

Javascript deleting current word from string and everything after it

I have a series of strings like this:

http://mytestdomain.com/bla/abc/?zde&wpv_paged=2
http://mytestdomain.com/bla/abc/?zde&wpv_paged=3
http://mytestdomain.com/bla/abc/?zde&wpv_paged=4
http://mytestdomain.com/bla/abc/?zde&wpv_paged=5

etc

I need a way to remove the '&wpv_paged=' part of the string as well as everything after it

My question is, should I use regex or split() for this and if so, how would I achieve this?

Thanks

&wpv_paged=\d+.*$

You can try this.Replace with empty string .See demo.

http://regex101.com/r/sU3fA2/7

var url = "http://mytestdomain.com/bla/abc/?zde&wpv_paged=2".split("&wpv_");

url[0]将为您提供url中“&wpv_”部分之前的部分

Depending on what you want for http://mytestdomain.com/bla/abc/?zde&wpv_paged=2&param2=abc

"http://mytestdomain.com/bla/abc/?zde&wpv_paged=2&param2=abc".split("&wpv_")[0]

gives:

http://mytestdomain.com/bla/abc/?zde

and

"http://mytestdomain.com/bla/abc/?zde&wpv_paged=2&param2=abc".replace(/&wpv_paged=\d+/, '')

gives:

http://mytestdomain.com/bla/abc/?zde&param2=abc

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