0

我一直在研究文本到 ASCII 文本转换器。这是代码:

(function (i , code) {
var inputs = code.getElementsByTagName("textarea"),
    text = inputs[0],
    binary = inputs[1],
    tran2 = /\s*[01]{8}\s*/g,
    e = /[\s\S]/g,
    f = /^(\s*[01]{8}\s*)*$/,
    r = /^[\x00-\xff]*$/,
    n = String.fromCharCode;
    ASC = '!"#$%&' + "'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
function m(s) {
    return "00000000".slice(String(s).length) + s
}
function change(s) {
    return s.replace(tran2, function (t) {
        return n(parseInt(t, 2))
    })
}
function change2(s) {
    return s.replace(e, function (t) {
        return m(t.charCodeAt().toString(2)) + ' '
    })
}
function h(object, regExp, func, SecObj) {
    var GotValue = object.value,
        s = "";
    if (regExp.test(GotValue)) {
        SecObj.value = s = func(GotValue);
        object.className = SecObj.className = ""
    } else {
        SecObj.value = "ERROR: invalid input";
        object.className = SecObj.className = "invalid"
    }
    return x == text ? GotValue : s
}
function primary() {
    var s = this == binary ? h(binary, f, change, text) : h(text, r, change2, binary);
}

text.onkeyup = binary.onkeyup = primary;
text.oninput = binary.oninput = function () {
    text.onkeyup = binary.onkeyup = null;
        primary.call(this)
    };
 }(this, document));

ThatWill 将 taxt 转换为二进制并返回。出现错误时,我想查找不匹配的字符

!"#$%&'()*+,-./0123456789:;<=>?
@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
`abcdefghijklmnopqrstuvwxyz{|}~

有没有人不知道我怎么能用 reg exp 做到这一点?

4

1 回答 1

2

以下将从字符串中删除所有合法字符,只留下您可以检查的非法字符

    var illegal = inputs[0].replace(/[!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\\\]\^_`abcdefghijklmnopqrstuvwxyz{|}~]/g, "");

    if (illegal.length > 0) {
        //move through and examine each illegal character..
    }
于 2013-09-08T03:09:07.740 回答