正则表达式以避免控制字符

时间:2022-09-29 00:07:27

I am working on flex and using regExp to check the value entered from UI. I want to ensure that entered value does not have any control characters and will give warning based on that. Since we support many languages, I can't have regex which have all possible positive values, thus I need to use blacklist control characters regular expression. I tried ^[^\x00-\x1F\x7F\u2028\u2029]*$ but it matches successfully if there is any regular character other than control character. I want it should return no match in case even a single control character is present. What should I change in this regular expression?

我正在使用flex并使用regExp检查从UI输入的值。我想确保输入的值没有任何控制字符,并根据该值发出警告。由于我们支持多种语言,因此我不能拥有具有所有可能正值的正则表达式,因此我需要使用黑名单控制字符正则表达式。我尝试了^ [^ \ x00- \ x1F \ x7F \ u2028 \ u2029] * $但如果除了控制字符之外还有任何常规字符,它会成功匹配。我想它应该返回没有匹配,以防即使存在单个控制字符。我应该在这个正则表达式中改变什么?

Will appreciate for the help.

非常感谢您的帮助。

1 个解决方案

#1


You can use the following trick (put your negated set in a lookahead followed by a . and capture as a whole):

你可以使用以下技巧(把你的否定集放在一个先行,然后是。并捕获整个):

^((?=[^\x00-\x1F\x7F\u2028\u2029]).)*$

#1


You can use the following trick (put your negated set in a lookahead followed by a . and capture as a whole):

你可以使用以下技巧(把你的否定集放在一个先行,然后是。并捕获整个):

^((?=[^\x00-\x1F\x7F\u2028\u2029]).)*$