简体   繁体   中英

How do I get the value before a certain value from a URL using jquery?

For Example:

www.site.com/potato/hello/eweb/TestPage.aspx
// or
www.site.com/potato/eweb/TestPage.aspx

I want to get everything before eweb from the URL using jQuery no matter how long the url before eweb is.

So from the example #1 above, I want to get www.site.com/potato/hello/ back and www.site.com/potato/ for example #2 as my result.

您可以通过eweb split()当前URL并获取结果数组的第一个元素:

var baseUrl = window.location.href.split('eweb')[0]; 

Try using String.prototype.match() with RegExp /.*(?=eweb)/ to match any characters followed by "eweb"

 var str = "www.site.com/potato/hello/eweb/TestPage.aspx"; var res = str.match(/.*(?=eweb)/)[0]; console.log(res) 

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