JS regex用于在URL中匹配不同的#标签。

时间:2022-12-17 16:50:34

I am in need of two regular expressions. First of all I want to check if my URL contains the hashtag #videos. Then if it does, I want to get the value of the second #tag. That value could contain all kinds of characters;

我需要两个正则表达式。首先,我要检查我的URL是否包含hashtag #视频。如果是的话,我想要得到第二个#标签的值。这个值可以包含各种字符;

http://local/default.php#videos#12345
http://local/default.php#videos#g5458f
http://local/default.php#videos#0-e4a5d

This is what I've got so far:

http://local/default。php #视频# 12345 http://local/default。php #视频# g5458f http://local/default。0-e4a5d这是我到目前为止得到的:

if (window.location.hash = 'videos') {
      var url = window.location.hash,
          id = url.match(regex-goes-here);  // output e.g string '12345'
}

(Not sure if my extremely simple check (window.location.hash = 'videos') will work on two hashtags..? There is probably a better way of checking the URL, if so, please do tell :-)

(不知道我是否做了非常简单的检查(window.location)。哈希= 'videos = 'videos = 'videos'中?可能有更好的方式检查URL,如果有,请告诉:-)

1 个解决方案

#1


3  

You can get an array of tags like this:

你可以得到这样的标签数组:

var tags = window.location.hash.replace(/^#/, '').split('#');

In case of http://local/default.php#videos#12345, tags[0] will be videos and tags[1] will be 12345. You can use tags.length to determine how many tags there are.

在http://local/default。php#视频#12345,标签[0]将是视频,标签[1]将是12345。您可以使用标签。长度来确定有多少个标签。

#1


3  

You can get an array of tags like this:

你可以得到这样的标签数组:

var tags = window.location.hash.replace(/^#/, '').split('#');

In case of http://local/default.php#videos#12345, tags[0] will be videos and tags[1] will be 12345. You can use tags.length to determine how many tags there are.

在http://local/default。php#视频#12345,标签[0]将是视频,标签[1]将是12345。您可以使用标签。长度来确定有多少个标签。