Javascript:如何删除最后一个条目

javascript: how to remove last entry?

本文关键字:最后一个 删除 何删除 Javascript      更新时间:2023-09-26

假设我有以下JS代码,当它接收到不可接受的字符时,我只能删除整个字符:

function checkInput() {
    document.getElementById("message").setAttribute('maxlength', (456));
    for (var i = 0; i < document.fr_upload.message.value.length; i++) {
        if (!checkLatin(document.fr_upload.message.value)) {
            alert("Your entry does not contain latin type.'n Please try again.")
            document.fr_upload.message.value = '';
            document.fr_upload.char_left.value = 0;
            return false;
        }
    }
}
function checkLatin(arg) {
    var latin = /^['u0020-'u007E]*$/;
    if (arg.match(latin)) {
        return true;
    } else {
        return false;
    }
}

那么,我怎样才能只删除不可接受的字符呢?

Try

function checkInput() {
    document.getElementById("message").setAttribute('maxlength', (456));
    var value = document.fr_upload.message.value;
    if (value && !/[^'u0020-'u007E]/.test(value)) {
        alert("Your entry contains non latin characters.'n Please try again.");
        document.fr_upload.message.value = value.replace(
                /[^'u0020-'u007E]/g, '');
        document.fr_upload.char_left.value = document.fr_upload.message.value.length;
    }
}

要替换非拉丁字符,可以使用:

function removeNonLatin(arg) {
    var nonlatin = /!(^['u0020-'u007E]*$)/g;
    arg = arg.replace(nonlatin , '');
    return arg;
}