![]() ![]() It allows you combine subexpressions as alternatives.įor example, (x|y|z)a will match xa or ya, or za. The pipe symbol means ‘or’, so it matches a series of options. Either of the subexpressions is called an alternative. Here, you use the pipe character | to match any of several subexpressions. What is Alternation?Īlternation is yet another way you can specify a set of options. You can write it as a range like this, and, respectively. Suppose you want to match a set of numbers, say, or a set of characters, say. For example: const regexPattern = /at/ Ĭonsole.log(regexPattern.test('bat')) // Output: falseĬonsole.log(regexPattern.test('cat')) // Output: falseĬonsole.log(regexPattern.test('mat')) // Output: trueĪ hyphen - indicates range when used inside a character class. If you add a caret symbol inside a character class like this, it will match any character that is not listed inside the square brackets. then i, then either e or a, then n, then c, and then e. The regex pattern interprets as: find a followed by m, then b, Let's look at an example: // Find and match a word with two alternative spellingsĬonsole.log(regexPattern.test('ambiance')) // Output: true To denote a character class, you use square brackets and then list the characters you want to match inside the brackets. \S – matches a non-white space character.Ī character class is used to match any one of several characters in a particular position.\W – matches any non-word (that is non-alphanumeric) character and is shorthand for.\D – matches any non-digit and is the same as.\s – matches any white space character.\w – matches any alphanumeric character which could be a letter, a digit, or an underscore.\d – matches any decimal digit and is shorthand for.In addition to the metacharacters we have looked at, here are some of the most commonly used ones: Search for a stand-alone word that begins and end with the pattern wardĬonsole.log(text3.match(regexPattern3)) // Output: Search for a word that ends with the pattern wardĬonsole.log(text2.match(regexPattern2)) // Output: Search for a word that begins with the pattern wardĬonsole.log(text1.match(regexPattern1)) // Output: The word is matched according to the position of the metacharacter. \b matches the start or end of a word.You use the metacharacters b and B to assert a word boundary. You can think of them as a word-based version of ^ and $. Word Boundaries are metacharacters that match the start and end position of a word – a sequence of alphanumeric characters. Note that anchors characters ^ and $ match just the position of the characters in the pattern and not the actual characters themselves. For example:Ĭonsole.log(regexPattern.test('The mouse and the cat')) // Output: trueĬonsole.log(regexPattern.test('The cat and mouse')) // Output: false $ matches the end of a line and anchors a literal at the end of that line.Without the ^ in the pattern, the output will return trueĬonsole.log(regexPattern2.test('The cat and mouse')) // Output: true For example:Ĭonsole.log(regexPattern1.test('cat and mouse')) // Output: trueĬonsole.log(regexPattern1.test('The cat and mouse')) // Output: false because the line does not start with cat ^ matches the start of a line and anchors a literal at the beginning of that line.You use them to assert where a boundary should be. Anchors and Boundaries:Īnchors are metacharacters that match the start and end of a line of text they are examining. Let's take a look at each subgroup and the characters that go with them. These characters fit into different subgroups that perform similar functions. Using special characters, you can do more than just find a direct match.įor example, if you want to match a character in a string that may or may not appear once or multiple times, you can do this with special characters. How to Use Regular Expression Special CharactersĪ special character in a regular expression is a character with a reserved meaning. One way to solve this could be: function isPattern(userInput) `, 'gi') Ĭonsole.log(str.match(regExpConst)) // Output: Here's a scenario: you want to verify that the telephone number entered by a user on a form matches a format, say, #-#-# (where # represents a number). With regular expressions, you can do things like find and replace text, verify that input data follows the format required, and and other similar things. Regular expressions are patterns that allow you to describe, match, or parse text. Let’s get to it! What Are Regular Expressions? In this article, you will learn about the fundamentals of regular expressions, regular expression pattern notation, how you can interpret a simple regex pattern, and how to write your own regex pattern. Knowing how to solve problems using regex is helpful to you as a developer and improves your productivity. They are key to efficient text processing. ![]() Regular expressions (regex) are a useful programming tool. ![]()
0 Comments
Leave a Reply. |