简体   繁体   English

正则表达式只匹配一组字符中的一个字符

[英]regex match a character from a set of characters only once

I have a following regular expression that works fine(with one exception that works fine, that I already know how to fix). 我有一个下面的正则表达式工作正常(一个异常工作正常,我已经知道如何解决)。 I would like to allow a user to prepend the input with > or <, but with a limit of 1. 我想允许用户在输入前加上>或<,但限制为1。

I don't know how to allow one character of a set of characters. 我不知道如何允许一组字符中的一个字符。

Here is the expression 这是表达

var valid = new RegExp('^-?['+rangeMin+'-'+rangeMax+']$');

Update: So there are two characters(>,<) a user can prepend(prefix) a value, he can enter > or <. 更新:因此,用户可以在前面加上两个字符(>,<),在该值前面加上前缀,可以输入>或<。 So if a user enters > he cant't enter > or < anymore and if he enters < he cant't enter > or <. 因此,如果用户输入>,则不能再输入>或<;如果输入<,则不能输入>或<。 Sorry guys, tried googling it but, that was a little to advance ;/ 抱歉,尝试过使用Google进行搜索,但是,这还有待改进; //

You may use 您可以使用

var valid = new RegExp('^[<>]?-?['+rangeMin+'-'+rangeMax+']$');

The [<>]? [<>]? is a character class that matches a single character, either > or < , 1 or 0 times due to ? 是与单个字符匹配的字符类 ,由于>< ,所以1或0次? quantifier. 量词。

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

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