简单的正则表达式问题
我想从 url 字符串中删除它 http://.....?page=1
我知道这行不通,但我想知道你如何正确地做到这一点。
document.URL.replace("?page=[0-9]", "")
谢谢
I want to remove this from a url stringhttp://.....?page=1
I know this doesn't work, but I was wondering how you would do this properly.
document.URL.replace("?page=[0-9]", "")
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来您想摆脱协议和查询字符串。那么,将其余部分连接起来怎么样?
http://jsfiddle.net/9Ng3Z/
我不完全确定要求是什么,但这相当简单的正则表达式有效。
这可能是一个幼稚的实现,但请尝试一下,看看它是否适合您的需要。
http://jsfiddle.net/9Ng3Z/1/
It seems like you want to get rid of the protocol and the querystring. So how about just concatenating the remaining parts?
http://jsfiddle.net/9Ng3Z/
I'm not entirely certain what the requirements are, but this fairly simple regex works.
It may be a naive implementation, but give it a try and see if it fits your need.
http://jsfiddle.net/9Ng3Z/1/
?
是正则表达式特殊字符。您需要将其转义为文字?
。还可以使用正则表达式文字。?
is a regex special character. You need to escape it for a literal?
. Also use regular expression literals.@patrick dw 的答案是最实用的,但如果您真的对正则表达式解决方案感到好奇,那么我会这样做:
The answer from @patrick dw is most practical but if you're really curious about a regular expression solution then here is what I would do:
你非常接近。要获取 URL,请使用 location.href 并确保转义问号。
您还可以删除所有查询字符串参数:
You're super close. To grab the URL use location.href and make sure to escape the question mark.
You can also strip all query string parameters: