更新uri hash javascript [英] Update uri hash javascript

查看:78
本文介绍了更新uri hash javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难相信没有被问到这个问题,但是我在任何地方都找不到参考.我需要添加URI哈希片段并更新值(如果它已经在哈希中).我目前可以获取它以添加哈希,但是我的正则表达式似乎无法捕获,因此它添加了另一个而不是更新.

    setQueryString : function() {
        var value = currentPage; 
        var uri = window.location.hash;
        var key = "page";
        var re = new RegExp("([#&])" + key + "=.*#(&|$)", "i");
        var separator = uri.indexOf('#') !== -1 ? "&" : "#";
        if (uri.match(re)) {
            return uri.replace(re, '$1' + key + "=" + value + '$2');
        }
        else {
            return uri + separator + key + "=" + value;
        }
    },

如果可以在保留其他很棒的URL值/哈希值的同时,使其变得更整洁.

根据要求输入示例

起始uri值: www.example.com#page = 1(或根本没有#page)

然后单击下一页" setQueryString,因此值将相等:

var value = 2;
var uri = '#page1'
var key = 'page'

所以希望的输出将是'#page2'.

解决方案

对于您的正则表达式问题,可以使用正则表达式#page=(number)或&page=(number)并捕获数字. >和.match(regex)方法.请注意,=需要在正则表达式中转义.

如果字符串中存在模式,则result将包含一个在result[1]处具有整数(作为字符串)的数组.如果该模式不存在,则result将为null.

 //match #page=(integer) or &page=(integer)

var test = "#foo=bar&page=1";
var regex = /[#&]page\=(\d*)/;
var result = test.match(regex);

console.log(result); 

如果要动态地将key=设置为"page"以外的其他内容,则可以动态构建正则表达式,如下所示(请注意,反斜杠需要转义字符串,使代码更复杂): /p>

 //dynamically created regex

var test = "#foo=bar&page=1";
var key = "page"
var regex = new RegExp("[#&]" + key + "\\=(\\d*)");
var result = test.match(regex);

console.log(result); 

I find it hard to believe this hasn't been asked but I can find no references anywhere. I need to add a URI hash fragment and update the value if it already is in the hash. I can currently get it to add the hash but my regex doesn't appear to catch if it exists so it adds another instead of updating.

    setQueryString : function() {
        var value = currentPage; 
        var uri = window.location.hash;
        var key = "page";
        var re = new RegExp("([#&])" + key + "=.*#(&|$)", "i");
        var separator = uri.indexOf('#') !== -1 ? "&" : "#";
        if (uri.match(re)) {
            return uri.replace(re, '$1' + key + "=" + value + '$2');
        }
        else {
            return uri + separator + key + "=" + value;
        }
    },

Also if this can be made any cleaner while preserving other url values/hashes that would be great.

example input as requested

Starting uri value: www.example.com#page=1 (or no #page at all)

then on click of "next page" setQueryString gets called so the values would equal:

var value = 2;
var uri = '#page1'
var key = 'page'

So the hopeful output would be '#page2'.

解决方案

As to your regex question, testing if the pattern #page=(number) or &page=(number) is present combined with capturing the number, can be done with the regex /[#&]page\=(\d*)/ and the .match(regex) method. Note that = needs escaping in regexes.

If the pattern exists in the string, result will contain an array with the integer (as a string) at result[1]. If the pattern does not exist, result will be null.

//match #page=(integer) or &page=(integer)

var test = "#foo=bar&page=1";
var regex = /[#&]page\=(\d*)/;
var result = test.match(regex);

console.log(result);

If you want to dynamically set the key= to something other than "page", you could build the regex dynamically, like the following (note that backslashes needs escaping in strings, making the code a bit more convoluted):

//dynamically created regex

var test = "#foo=bar&page=1";
var key = "page"
var regex = new RegExp("[#&]" + key + "\\=(\\d*)");
var result = test.match(regex);

console.log(result);

这篇关于更新uri hash javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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