查明字符串是否由特定字符集组成
如果字符串仅包含一组特定字符: {
AZ
和 }
,我该如何判断?
例如
{VARIABLE}
=>应该返回 true{VARiABLE}
=> 里面有一个小写的i
- 应该是 false,因为
{ VARIABLE}
=> 应该为 false,因为有空格等。
哦,非常重要:
字符串在 {
和 }
之间必须至少有一个字符,因此:
{}
也应该是 false...
How can I out if a string only contains a certain set of characters: {
A-Z
and }
?
For example
{VARIABLE}
=> should return true{VARiABLE}
=> should be false, because there's a lowercasei
inside{ VARIABLE}
=> should be false because there's a space etc.
Oh, very important:
the string MUST have at least one character between {
and }
, so:
{}
should be false too...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Jquery 代码:
HTML 代码:
如果值为 {UPPERCASELETTERS},它将返回警报(“这是正确的值,是的,它是正确的”)
Jquery Code:
HTML Code:
It will return alert("It's the correct value, yes it's right"), if value is {UPPERCASELETTERS}
试试这个正则表达式...
Try this regex...
使用这个表达方式。
[AZ{}]*
这里的方括号 [] 坚持要出现的字符,* 表示该模式可以重复多次。
Use this expression.
[A-Z{}]*
Here the square brackets [] insist on what characters to be present and * says that this patter can repeat multiple times.
进行负正则表达式匹配。如果您匹配
/[^AZ{}]/
之类的内容并获得成功,则该字符串包含“不允许”的内容。Do a negative regex match. If you match something like
/[^A-Z{}]/
and get a success, then the string contains something that's "not allowed".使用此正则表达式:
^[AZ{}]+$
。它仅允许AZ
和{}
Use this regex:
^[A-Z{}]+$
. It allows onlyA-Z
and{}
这听起来像是使用正则表达式的好例子。特别是,正则表达式允许匹配一系列字符 -
[AZ{}]
将匹配任何大写字母、{
或} 字符
。根据新要求进行编辑 - 您希望匹配以文字
{
开头的内容,然后在范围内至少有一个字符>AZ
,然后是结束}
。这给出了正则表达式:因此您可以匹配整个正则表达式:
This sounds like a good case to use regular expressions. In particular, regexes allow one to match a range of characters -
[A-Z{}]
would match any character which is either an uppercase letter,{
, or}
.EDIT based on new requirements - you want to match something that starts with a literal
{
, then has at least one character in the rangeA-Z
, then a closing}
. Which gives the regex:Thus you could match against the entire regex:
在这种情况下,请使用:
正则表达式表示以下格式的任何字符串:
{
}
^...$< /code> 确保字符串应该完全是这种形式,而不仅仅是子字符串(否则
test{AAA}
也会匹配)。In that case use:
The regexp represents any string of the format:
{
}
The
^...$
makes sure that the string should be exactly of this form, rather than a substring only (otherwisetest{AAA}
would match too).