【问题标题】:javascript use RegExp to find outcasts [closed]javascript 使用 RegExp 查找弃儿 [关闭]
【发布时间】:2013-09-11 20:38:18
【问题描述】:

我一直在研究文本到 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 做到这一点吗?

【问题讨论】:

  • 您的意思是要查找输入字符串中包含非法字符的位置吗?
  • @Robert Byrne 是的,那也行。
  • 嗯,好吧,假设您只想验证输入是否仅包含合法字符,请参阅发布的答案

标签: javascript regex binary ascii


【解决方案1】:

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

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

    if (illegal.length > 0) {
        //move through and examine each illegal character..
    }

【讨论】:

  • 那能告诉我哪些角色是弃儿吗?
  • 好的,所以您只需要不在合法集中的字符?
  • 是的,我想要那些不在合法集合中的字符。
猜你喜欢
  • 1970-01-01
  • 2013-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-31
  • 1970-01-01
相关资源
最近更新 更多