![]() |
In this article, we will see how to search a vertical tab character with JavaScript RegExp. A vertical tab character (VT) is a control character used to cause a printer or display device to move its output up by one line. It has an ASCII code of 11 or 0x0B. Syntax: The syntax for searching a vertical tab character using a regular expression in JavaScript is: /[\v]/g This regular expression uses the character class `[\v]` to match any vertical tab character in the input string. The `g` flag is used to perform a global search, which means that all matches will be found, not just the first one.
Approaches: To search for a vertical tab character with JavaScript RegExp, there are a few different approaches you can use: Use the vertical tab escape sequence `\v`: The simplest way to search for a vertical tab character is to use the vertical tab escape sequence `\v` in your regular expression. const regex = /\v/; Use the Unicode code point for the vertical tab character const regex = /\u000B/; Use a character class to match any whitespace character: If you want to match any whitespace character, including the vertical tab character, you can use a character class like `[ \t\r\n\v\f]`. const regex = /[ \t\r\n\v\f]/; Example 1: Using the RegExp.test() method: The RegExp.test() method returns true if the regular expression matches the input string, and false otherwise. Here’s an example that searches for a vertical tab character in a string using RegExp.test() method. Javascript
Output: True Example 2: Using the String.search() method: The String.search() method searches for a regular expression match in a string and returns the index of the first match, or -1 if no match is found. Here’s an example that searches for a vertical tab character in a string using String.search() method. Javascript
Output: 5 Example 3: Using the String.match() method: The String.match() method searches for a regular expression match in a string and returns an array of the matches, or null if no match is found. Here’s an example that searches for a vertical tab character in a string using String.match() method. Javascript
Output: [ '\v', index: 5, input: 'Hello\vworld!' ] Example 4: Using the RegExp.exec() method: The RegExp.exec() method searches for a regular expression match in a string and returns an array of the matches, or null if no match is found. Here’s an example that searches for a vertical tab character in a string using RegExp.exec() method. Javascript
Output: [ '\v', index: 5, input: 'Hello\vworld!' ] Note: The above examples assume that the input string contains a vertical tab character. If the input string does not contain a vertical tab character, the RegExp search methods will return false, -1, null, or undefined depending on the method used. |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |