Find out if a character string is composed

How can I exit if the string contains only a specific character set: { AZ and } ?

for instance

  • {VARIABLE} => should return true
  • {VARIABLE} => must be false because inside <<25> inside 25>
  • { VARIABLE} => must be false because there is a space, etc.

Oh, very important:

the line MUST have at least one character between { and } , therefore:

  • {} must also be false ...
+4
source share
7 answers

In this case, use:

 /^{[AZ]+}$/.test(str); 

A regular expression represents any format string:

  • First a {
  • Then one or more capital letters
  • Then a }

^...$ guarantees that the string must be of this particular form, and not just a substring (otherwise test{AAA} will also match).

+6
source

This sounds good for using regular expressions. In particular, regular expressions allow you to match a range of characters - [AZ{}] will match any character that is either an uppercase letter, { , or } .

EDIT based on new requirements - you want to match something that starts with the literal { , and then has at least one character in the AZ range, and then close } . Which gives a regex:

 {[AZ]+} 

This way you can match the entire regex:

 val input = "{VARIABLE}" return input.test(/{[AZ]+}/) // returns true "{VARiABLE}".test(/{[AZ]+}/) // returns false "{ VARIABLE}".test(/{[AZ]+}/) // returns false "".test(/{[AZ]+}/) // returns false - open bracket didn't match "{}".test(/{[AZ]+}/) // returns false - AZ part didn't match 
+6
source

Use this regex: ^[AZ{}]+$ . It allows you to use only AZ and {}

+2
source

Make a negative regex. If you match something like /[^AZ{}]/ and get success, then the line contains something that is "not allowed."

+2
source

Try this regex ...

 /^[{}AZ]+$/ /^[{}AZ]+$/.test("{VARIABLE}") // => true 
+1
source

Use this expression.

[AZ {}] *

Here the square brackets [] insist on what characters are present, and * says that this patter can be repeated several times.

+1
source

Jquery Code:

 $(document).ready( function(){ $('#test_regex').click( function(){ regex= /^{[AZ]+}$/; str= $('#reginput').val(); result= regex.test(str); if( result ){ alert("It the correct value, yes it right"); }else{ alert("It incorrect value.. You know"); } }); }); 

HTML code:

 <input type="text" id="reginput"/> <button id="test_regex">Check Value</button> 

It will return a warning ("This is the correct value, yes, it is correct") if the value is {UPPERCASELETTERS}

+1
source

All Articles