

Thus, the string "abc:xyz" would match, but the string "acb:xyz" would not. In between the subexpressions, we have a colon ( :). Similarly, the second subexpression is looking for "xyz". The first subexpression is looking for a part of the string that matches the string "abc" exactly.

The following example contains two grouping constructs or subexpressions: Each section within parentheses is known as a subexpression. The primary way you group a section of a regex is by using parentheses ( ()). To break these sections up, you'll need to use grouping constructs. As regular expressions grow more complicated, you may check multiple parts of a string to determine that different sections fulfill different requirements. The “Matching a Username” regex is fairly straightforward and open-ended about what it accepts. Although it's beyond the scope of this tutorial to learn everything, let’s touch on a few other regex components that you might encounter. But is that all there is to learn about regular expressions? This actually only scratches the surface of what we can accomplish by using a regex. Great! We've successfully explained the “Matching a Username” regex. l3rn*antin0?1 includes the special characters * and ?, which aren't allowed in this regex. l3rn-antin0_am1k0 has 17 characters, and the maximum limit for this regex is 16. 元RN-antino0_1 includes uppercase characters. The 25 string is not a match because it only contains 2 characters and the minimum is 3. Remember that because our regex pattern is entirely inside a bracket expression, the string doesn’t need to meet all of the requirements-just any of them. l3rn-antin0_1 is a match because it is 13 characters long (within the minimum and maximum character limits of 3 and 16) and includes a combination of lowercase characters, numbers, and the special characters of an underscore and a hyphen. The 123 string is a match because it meets the minimum requirement of 3 characters, and the characters are all numbers. This regex is looking for any string between 3 and 16 characters that starts and ends with a combination of lowercase characters, the numbers 0–9, and the special characters of an underscore and a hyphen. Because our bracket expression ( ) will match any string that includes any combination of lowercase letters between a and z, any number between 0 and 9, and the special characters of an underscore or a hyphen, this quantifier means that this string has to be between 3 and 16 characters. This means that we want to find the preceding string pattern a minimum of 3 times and a maximum of 16 times. You'll notice that we didn't include the pattern. So in our “Matching a Username” regex, the string must start and end with something that matches the pattern. Just as with the ^ character, it can be preceded by an exact string or a range of possible matches. The $ anchor signifies a string that ends with the characters that precede it. This is because a regex is case-sensitive.Ī range of possible matches, displayed using bracket expressions. This could be in one of two formats:Īn exact string match, such as ^The, where the strings "The" or "The person" match, but "the" and "the person" do not. The ^ anchor signifies a string that begins with the characters that follow it. The characters ^ and $ are both considered to be anchors. Now let's take a look at the components of a regex.

To learn more, review the MDN Web Docs on the RegExp object.

The constructor function's parameters are not enclosed within slashes instead, they use quotation marks. The second is to use a RegExp constructor. The first, shown in our example, uses literal notation. Note: JavaScript provides two ways to create a regex object.
