In JavaScript, the \uxxxx expression is used to test, match, and search Unicode characters represented by hexadecimal numbers. By employing regular expressions in a single line of code, JavaScript can extract only words from a given string. This capability is beneficial for validating input in both web and desktop applications developed with JavaScript.
Syntax
- Syntax without modifiers
This syntax is utilized to retrieve the Unicode character associated with the hexadecimal number.
/\uxxxx/
- Syntax with modifiers
The syntax is employed to obtain the hexadecimal number with specified modifiers.
/\uxxxx/g
Supported Browsers
The javascript \uxxxx regex Metacharacter supports the following browsers supported using the javascript function:
- Google Chrome browser
- Safari browser
- Mozilla Firefox
- Opera browser
- Internet Explorer browser
Examples
Below are instances demonstrating how to search for, match, test, and execute the metacharacter representing word characters in regular expressions using JavaScript.
Utilize the test function along with the regular expression flag.
In the JavaScript code snippet below, hexadecimal values are represented using Unicode characters with the help of regex. The test method is employed to obtain the Boolean result of the value.
<!DOCTYPE html>
<html>
<head>
<title>
JavaScript RegExp \uxxx (hexadecimal) Metacharacter
</title>
</head>
<body>
<h3>
JavaScript RegExp \uxxx (hexadecimal) Metacharacter
</h3>
<h4> We can use the validation for the hexadecimal input values and shows in the Unicode character.
</h4>
<div id = "regex_information"></div>
<button onclick = "uni_display();"> Click Here!</button>
<script>
function uni_display() {
let input_pattern = /\u0075/g;
var input_regex1 = input_pattern.test('Hello Student');
var input_regex2 = input_pattern.test('you can learn');
var input_regex3 = input_pattern.test('ur new subject');
var regex_ele = document.getElementById("regex_information");
regex_ele.innerHTML = " Hello Student : "+input_regex1+"<br> you can learn : "+input_regex2+" <br> ur new subject : "+input_regex3
}
</script>
</body>
</html>
Output
The result indicates the accessibility of the hexadecimal input sequence.
Example2: Utilize the search function without employing the regex modifier.
In this instance, regular expressions are utilized solely for the string without employing any modifiers.
<!DOCTYPE html>
<html>
<head>
<title>
JavaScript RegExp \uxxx (hexadecimal) Metacharacter
</title>
</head>
<body>
<h3>
JavaScript RegExp \uxxx (hexadecimal) Metacharacter
</h3>
<h4> We can use the validation for the hexadecimal input values and shows in the Unicode character.
</h4>
<div id = "regex_information"></div>
<button onclick = "uni_display();"> Click Here!</button>
<script>
function uni_display() {
let input_pattern = /\u0075/;
var input_val1 = 'Hello Student';
var input_val2 = 'you can learn';
var input_val3 = 'ur new subject';
var input_regex1 = input_val1.match(input_pattern);
var input_regex2 = input_val2.match(input_pattern);
var input_regex3 = input_val3.match(input_pattern);
var regex_ele = document.getElementById("regex_information");
regex_ele.innerHTML = " Hello Student : "+input_regex1+"<br> you can learn : "+input_regex2+" <br> ur new subject : "+input_regex3
}
</script>
</body>
</html>
Output
The result displays the Unicode representation of the hexadecimal input string.
Illustrative Example 3: Apply the exec function with the regular expression flag.
In this instance, regular expressions are exclusively employed to search the complete input string with a global modifier. This approach ensures that the entire input string is thoroughly examined.
<!DOCTYPE html>
<html>
<head>
<title>
JavaScript RegExp \uxxx (hexadecimal) Metacharacter
</title>
</head>
<body>
<h3>
JavaScript RegExp \uxxx (hexadecimal) Metacharacter
</h3>
<h4> We can use the validation for the hexadecimal input values and shows in the Unicode character.
</h4>
<div id = "regex_information"></div>
<button onclick = "uni_display();"> Click Here!</button>
<script>
function uni_display() {
let input_pattern = /\u0075/g;
var input_regex1 = input_pattern.exec('Hello Student');
var input_regex2 = input_pattern.test('you can learn');
var input_regex3 = input_pattern.test('ur new subject');
var regex_ele = document.getElementById("regex_information");
regex_ele.innerHTML = " Hello Student : "+input_regex1+"<br> you can learn : "+input_regex2+" <br> ur new subject : "+input_regex3
}
</script>
</body>
</html>
Output
The result displays the Unicode character corresponding to the hexadecimal input string.
Example4: use the match method with the regex
The instance compares the hexadecimal representation of the string with the global flag.
<!DOCTYPE html>
<html>
<head>
<title>
JavaScript RegExp \uxxx (hexadecimal) Metacharacter
</title>
</head>
<body>
<h3>
JavaScript RegExp \uxxx (hexadecimal) Metacharacter
</h3>
<h4> We can use the validation for the hexadecimal input values and shows in the Unicode character.
</h4>
<div id = "regex_information"></div>
<button onclick = "uni_display();"> Click Here!</button>
<script>
function uni_display() {
let input_pattern = /\u0075/g;
var input_val1 = 'Hello Student';
var input_val2 = 'you can learn;
var input_val3 = 'ur new subject';
var input_regex1 = input_val1.match(input_pattern);
var input_regex2 = input_val2.match(input_pattern);
var input_regex3 = input_val3.match(input_pattern);
var regex_ele = document.getElementById("regex_information");
regex_ele.innerHTML = " Hello Student : "+input_regex1+"<br> you can learn : "+input_regex2+" <br> ur new subject : "+input_regex3
}
</script>
</body>
</html>
Output
The result displays the provided values represented in Unicode using the hexadecimal notation.
Conclusion
The values provided by the user will be transformed into Unicode characters representing hexadecimal values before being shown. Each hexadecimal value comprises four characters and is validated using the regular expression \u for user input verification.