转换并检查表示页码的字符串

时间:2016-07-18 11:55:59

标签: javascript

我需要检查一个表示页码的字符串是否有效;否则,如果可能,应该调整字符串。

有效的网页编号可以是1233-40

首先,我删除空格以使33 - 40也有效。我也在替换特殊破折号。

value = value.replace(/\u2013|\u2014/g, '-').replace(/\s+/g, '');

特别之处在于输入格式为530-5,应转换为530-535530-41应获得530-541。我不知道如何完成这件事。

接下来,如果只有数字或短划线,我会做一个测试。也许这不是最好的方式?!

value = /^[0-9\-]*$/.test(value) ? value : undefined;

实施例

342         // valid
0           // invalid
12-15       // valid
12-5        // gets 12-15
15-12       // invalid
115-20      // gets 115-120
115 -  20   // gets 115-120
-20         // invalid

2 个答案:

答案 0 :(得分:2)

  

特别之处在于输入可以采用530-5格式,应该转换为530-535。 530-41应该得到530-541。我不知道如何完成这件事。

以下功能应该可以解决这个问题:

Plnkr

<script>
    function getValidRange(pgNo) {
        var pgNoS = pgNo.split('-');

        var len1 = pgNoS[0].toString().length;
        var len2 = pgNoS[1].toString().length;
        var multiplier = 1;

        var newPage = "";

        if (Number(len1) > Number(len2)) {
            for (var i = 0; i < len1 - len2; i++) {
                newPage = newPage + pgNoS[0].toString()[i] + "";
            }
            multiplier = Math.pow(10, pgNoS[1].toString().length);
            pgNoS[1] = newPage + pgNoS[1];
        }
        if (Number(pgNoS[1]) < Number(pgNoS[0])) {
            pgNoS[1] = Number(pgNoS[1]) + multiplier;
        }
        return pgNoS[0].toString() + '-' + pgNoS[1].toString();
    }

    document.write(getValidRange('12-5') + "<br>");
    document.write(getValidRange('530-41') + "<br>");
    document.write(getValidRange('530-28') + "<br>");
    document.write(getValidRange('536-5') + "<br>");
    document.write(getValidRange('5312328-5') + "<br>");
    document.write(getValidRange('5312328-345') + "<br>");
    document.write(getValidRange('5312328-1211') + "<br>");
</script>

<强>输出

12-15
530-541
530-628
536-545
5312328-5312335
5312328-5312345
5312328-5321211

P.S。:代码未针对极端情况进行测试。随意指出任何错误/错误,我会为您解决。

答案 1 :(得分:1)

// create function to return normalised pagination
function getPages(i){
    // match pattern to extract pages from valid strigs
    // http://regexper.com/#/^\s*(\d+)\s*(?:(?:\u2013|\u2014|-)\s*(\d+)\s*)?$/
    var m = i.match(/^\s*(\d+)\s*(?:(?:\u2013|\u2014|-)\s*(\d+)\s*)?$/);
    // if there is a match then validate, else return null immediately
    if(m !== null && parseInt(m[1])) {
        // get references to first and second page numbers
        var from = parseInt(m[1]),
            to = parseInt(m[2]);
        // if there is a from and to page and to is greater than from...
        if(to && to > from) return from + '-' + to;
        // if there is from and to page and to is order of magnitude less than from
        else if(to && m[2].length < m[1].length) return from + '-' + (m[1].substr(0, m[1].length - m[2].length) + to);
        // if there is only from
        else if(!to) return from + '';
        // finally, if nothing else matched, return null
        else return null
    } else {
        return null;
    }
}

['342', '0', '12-15', '12-5', '15-12', '115-20', '115 -  20', '-20'].forEach(i => console.log(getPages(i)))

正则表达式的解释...... enter image description here