jQuery检查并从读取的URL末尾删除斜杠 [英] JQuery check and remove slash from the end of URL read

查看:224
本文介绍了jQuery检查并从读取的URL末尾删除斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jQuery脚本中,我有一行代码可获取当前URL的字符串:

In a jQuery script I have the line of code which gets the string of current URL:

var target = $(this).attr('href');

此脚本中的代码可以检查URL字符串末尾是否有斜杠.如果存在,则将其删除?您可以建议采用哪种方式?

Could the code in this script check if there is a slash at the end of the URL string. If it is present then remove it? What is the way to do it, you could recommend?

推荐答案

我会这样做:

 target = target.replace(/\/$/, '');

现在,如果您需要担心查询字符串的存在:

Now if you need to worry about the presence of a query string:

 <a href='http://foo.bar.com/something/?param=xyz'>hi</a>

然后事情变得有些棘手.使用正则表达式解析URL可能是可行的,但这很混乱.如果您可以避免这种情况,请缩小URL外观的可能性,从而不必使用一些巨大的正式"模式.

then things get a little more tricky. Parsing a URL with a regex is probably possible, but it's pretty messy. If you can get away with it, narrow down the possibilities of what your URLs can look like so that you don't have to use some big huge "official" pattern.

这篇关于jQuery检查并从读取的URL末尾删除斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆