Square brackets are used in JavaScript Regular Expressions to create a character class. Character classes are based on a grouping of literal characters. For example, /[joey]/ would evaluate to true if any of the literal characters ‘j’, ‘o’, ‘e’ or ‘y’ is found in the test string:
//The following evaluates to true because it contains a ‘j’
var IsFound = /[joey]/.test(”abcj”);
alert (IsFound);
//The following evaluates to false because it does not contain ‘j’, ‘o’, ‘e’ or ‘y’
var IsFound = /[joey]/.test(”abcd”);
alert (IsFound);
Filed under: JavaScript, Programming, Web