简体   繁体   English

有没有更简洁的方法来获取我网址中的最后一个数字?

[英]Is there a more succinct way to get the last number in my url?

So I currently pass two variables into the url for use on another page. 因此,我目前将两个变量传递到url中,以便在另一页上使用。 I get the last variable (ie #12345) with location.hash. 我得到与location.hash的最后一个变量(即#12345)。 Then from the other part of the url (john%20jacob%202) all I need is the '2'. 然后从URL的另一部分(john%20jacob%202),我需要的只是'2'。 I've got it working but feel there must be a cleaner and succinct way to handle this. 我已经开始工作了,但是觉得必须有一种更简洁的方法来解决这个问题。 The (john%20jacob%202) will change all the time to have different string lengths. (john%20jacob%202)将始终更改为具有不同的字符串长度。

url: http://localhost/index.html?john%20jacob%202?#12345

<script>
    var hashUrl = location.hash.replace("?","");

       // function here to use this data

    var fullUrl = window.location.href;
    var urlSplit = fullUrl.split('?');
    var justName = urlSplit[1];
    var nameSplit = justName.split('%20');
    var justNumber = nameSplit[2];

       // function here to use this data

</script>

A really quick one-liner could be something like: 一个真正快速的单线可能是这样的:

let url = 'http://localhost/index.html?john%20jacob%202?#12345';

url.split('?')[1].split('').pop();

// returns '2' 

How about something like 怎么样

decodeURI(window.location.search).replace(/\D/g, '')

Since your window.location.search is URI encoded we start by decoding it. 由于您的window.location.search是URI编码的,因此我们首先对其进行解码。 Then replace everything that is not a number with nothing. 然后用数字代替所有不是数字的东西。 For your particular URL it will return 2 对于您的特定URL,它将返回2


Edit for clarity : 编辑为清晰起见

Your example location http://localhost/index.html?john%20jacob%202?#12345 consists of several parts, but the interesting one here is the part after the ? 您的示例位置http://localhost/index.html?john%20jacob%202?#12345包含几部分,但有趣的是,在?之后的部分? and before the # . #之前。

In Javascript this interesting part, the query string (or search ), is available through window.location.search . 在Javascript中,可通过window.location.search获得此有趣的部分,即查询字符串(或search )。 For your specific location window.location.search will return ?john%20jacob%202? 对于您的特定位置, window.location.search将返回?john%20jacob%202? .

The %20 is a URI encoded space. %20是URI编码的空间。 To decode (ie. remove) all the URI encodings I first run the search string through the decodeURI function. 为了对所有URI编码进行解码(即删除),我首先通过encodeURI函数运行search字符串。 Then I replace everything that is not a number in that string with an empty string using a regular expression. 然后,我使用正则表达式将该字符串中不是数字的所有内容替换为空字符串。

The regular expression /\\D/ matches any character that is not a number, and the g is a modifier specifying that I want to match everything (not just stop after the first match), resulting in 2 . 正则表达式/\\D/匹配不是数字的任何字符,并且g是一个修饰符,指定我要匹配所有内容(不仅仅是在第一次匹配之后停止),结果为2

If you know you are always after a tag, you could replace everything up until the "#" 如果您知道自己一直在标签后面,则可以替换所有内容,直到“#”

url.replace(/^.+#/, '');

Alternatively, this regex will match the last numbers in your URL: 另外,此正则表达式将匹配URL中的最后一个数字:

url.match(/(?<=\D)\d+$/);

//(positive look behind for any non-digit) one more digits until the end of the string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM