This post will illustrate the objective, usage, and working of the “\b” metacharacter in the RegExp of JavaScript.
What Does “\b” Metacharacter Do in RegExp of JavaScript?
The “\b” metacharacter in JavaScript relies on the pattern position i.e., before and after. To find the pattern at the starting position, specify it after the “\b” metacharacter, and for the end position, write it before the “\b”.
Syntax (Basic)
The basic syntax of “\d” contains:
- /(Forward-slash): Specifies the boundaries of the Regular Expression and is equivalent to /[\d]/.
- \(Backslash): Escapes the backslash and treats the next mentioned character as a metacharacter.
- d: Matches the pattern placed at the beginning or end of the input string.
Syntax (With Modifiers)
In this syntax:
- g(global): Searches globally and finds all the matches. It does not stop after the first match.
- i(case-insensitive): Ignores the case sensitivity.
- m(multiple): Species the multiline line search and is only restricted at “^(start of the string)” and “$(end of the string)”.
Syntax(With RegExp() Constructor)
The “\b” metacharacter can also be utilized as a RegExp object with the “RegExp()” constructor:
In the above syntax:
- new: Represents the operator or keyword that creates an object from a constructor.
- RegExp(): Denotes the constructor that works on the “\\b” metacharacter in the form of the first parameter/argument.
Example 1: Applying the “\b” Metacharacter to Search Specific Pattern at Start Position Based on Modifier Syntax(/\b/g)
This example explains the working of the “\b” metacharacter with the additional support of the “g(global)” modifier to search the specified pattern globally at the start of the specified string.
HTML Code
Firstly, overview the following HTML code:
<p> String: Welcome to Linuxhint Website </p>
<button onclick="search()">Click it!</button>
<p id="sample"></p>
In the above code:
- The “<h2>” tag defines the first subheading.
- In the next step, the “<p>” tag creates a paragraph specifying the stated string.
- The “<button>” tag adds up a button having an “ondblclick” event redirecting to the function “find()” which will be triggered upon the button double-click.
- After that, the “<p>” denotes the second empty paragraph assigned an id “sample” to display the searched pattern.
JavaScript Code
Next, consider the following JavaScript code:
function search() {
var str = "Welcome to Linuxhint Website";
var text = /\bLi/g;
var match = str.match(text);
document.getElementById("sample").innerHTML = "Total " + match.length + " matches found in the given string:" + match;
}
</script>
In the above lines of code:
- Firstly, define the function “search()”.
- In the function definition, a variable “str” of data type “var” is declared that stores the stated string enclosed in double quotes.
- The second variable “text” defines the syntax of the “\b” metacharacter with the “Li” pattern and the “g” global search flag/modifier. It is such that it will find the “Li” pattern at the start of each word present in the specified string.
- After that, associate the “match()” method to match the “Li” pattern using its value “/\bLi/g” from the string.
- In the end, the “document.getElementById()” method will fetch the paragraph via its id “sample” to display the total matches of the specified word i.e., “Li” using the “length” property.
Output
The output shows that there is only a “1(Li)” match found in the specified string.
Example 2: Applying the “\b” Metacharacter to Search Specific pattern at the End Position Based on Modifier Syntax(/\b/g)
To find the specific pattern at the end of the string then specify it before the placement of “\b” in its syntax like this “/pattern\b/g”. Keeping this in view, we will find the “We” pattern at the end of the input string.
The HTML code is the same as in Example 1. So, move on to the JavaScript code.
JavaScript Code
The modified JavaScript code is written here:
function search() {
var str = "Welcome to LinuxhintWe WebsiteWe";
var text = /We\b/g;
var match = str.match(text);
document.getElementById("sample").innerHTML = "Total " + match.length + " matches found in the given string: " + match;
</script>
In the above code block, the “/We” pattern is specified at the beginning of the “\b” metacharacter to “g(globally)” search it from the specified string via the discussed “match()” method.
Output
The output displays that “2” matches of “We” are found at the end of the given input string upon button double-click.
Conclusion
JavaScript RegExp provides the “\b” metacharacter that sets the word boundary to match the specified pattern from the start and end of the string. Like other metacharacters, it also stops at the first match and returns its value. To search the string completely, it can also be utilized with an additional “g(global)” modifier. This post provided a brief description of the working of the “\b” metacharacter in RegExp of JavaScript.