简体   繁体   English

JavaScript - 正则表达式删除代码/特殊字符/数字等

[英]JavaScript - Regex to remove code / special characters / numbers etc

Answer @Wiktor Stribiżew suggested:回答@Wiktor Stribiżew 建议:

function myValidate(word) {
    return (word.length === 1 || /[^A-Z]/i.test(word)) ? true : false;
    }

Hello during the creation of an array I have a function that will not allow words with certain characters etc to be added to the array您好,在创建数组的过程中,我有一个函数不允许将具有某些字符等的单词添加到数组中

    function myValidate(word) {
        // No one letter words
        if (word.length === 1) {
            return true;
        }
        if (word.indexOf('^') > -1 || word.indexOf('$') > -1) {
            return true;
        }
        return false;
    }

It seems like not the proper way of going about this and ive been looking into a regex that would handle it but have not been successful implementing it, tried numerous efforts like:这似乎不是解决这个问题的正确方法,我一直在研究一个可以处理它但没有成功实施它的正则表达式,尝试了很多努力,例如:

if (word.match('/[^A-Za-z]+/g') ) {
            return true;
        }

can some one shed some light on the proper way of handling this?有人可以阐明处理此问题的正确方法吗?

I suggest using a simpler solution:我建议使用更简单的解决方案:

 function myValidate(word) { return (word.length === 1 || /[^AZ]/i.test(word)) ? false : true; } var words = ["Fat", "Gnat", "x3-2741996", "1996", "user[50]", "definitions(edit)", "synopsis)"]; document.body.innerHTML = JSON.stringify(words.filter(x => myValidate(x)));

Where:在哪里:

  • word.length === 1 checks for the string length word.length === 1检查字符串长度
  • /[^AZ]/i.test(word) checks if there is a non-ASCII-letter symbol in the string /[^AZ]/i.test(word)检查字符串中是否存在非 ASCII 字母符号

If any of the above condition is met, the word is taken out of the array.如果满足上述任何条件,则从数组中取出单词。 The rest remains.剩下的就剩下了。

EDIT: using test instead of match编辑:使用test而不是match

You want to use test() because it returns a bool telling you if you match the regex or not.您想使用test()是因为它返回一个布尔值,告诉您是否匹配正则表达式。 The match() , instead, always returns the matched elements.相反, match()总是返回匹配的元素。 Those may be cast to true by coercion.这些可以通过强制转换为true This is not what you want.这不是你想要的。

To sum it all up you can just use this one-liner (no if needed and no quotes either, cannot get any simpler):总而言之,你可以只使用这个单行( if需要,也没有引号,不能再简单了):

return word.test(/^[a-zA-Z][a-zA-Z]+$/); // two letter words

You should whitelist characters instead of blacklisting.您应该将字符列入白名单而不是列入黑名单。 That's one of the principles in security.这是安全的原则之一。 In your case, don't tell what is wrong, but tell what is right:在你的情况下,不要告诉什么是错的,而要告诉什么是对的:

if (word.test('/^[a-zA-Z]+$/')) { // two letter words
    return false;
}

This will return false for all words that contain ONLY [a-zA-Z] characters.对于仅包含[a-zA-Z]字符的所有单词,这将返回 false。 I guess this is what you want.我想这就是你想要的。

Your regex, instead, looked for illegal characters by negating the character group with the leading ^ .相反,您的正则表达式通过使用前导^否定字符组来查找非法字符。


Two recommendations:两个建议:

  1. Just use regex in a positive way (without negation) and it'll be a lot easier to understand.只需以积极的方式(没有否定)使用正则表达式,它就会更容易理解。

  2. Also, validation functions normally return true for good data and false for bad data.此外,验证函数通常对好的数据返回true ,对坏的数据返回false

It is more readable this way:这种方式更具可读性:

if (validate(data))
{
    // that's some good data we have here!
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM