Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
With JavaScript RegExp search a hexadecimal number character.
To find a hexadecimal number character with JavaScript Regular Expression, use the \x escape sequence followed by the two-digit hexadecimal value.
Syntax
\xdd
Where dd represents the two-digit hexadecimal value (00-FF) of the character you want to match.
Example: Searching for Hexadecimal Character
The following example searches for hexadecimal number 53, which represents the character 'S':
<html>
<head>
<title>JavaScript Regular Expression</title>
</head>
<body>
<script>
var myStr = "Secure and Responsive!";
var reg = /\x53/g;
var match = myStr.match(reg);
document.write(match);
</script>
</body>
</html>
S
Common Hexadecimal Characters
Here are some commonly used hexadecimal character codes:
<html>
<head>
<title>Hexadecimal Character Examples</title>
</head>
<body>
<script>
var text = "Hello World! 123";
// \x48 = 'H' (72 in decimal)
var matchH = text.match(/\x48/g);
document.write("Found 'H': " + matchH + "<br>");
// \x6F = 'o' (111 in decimal)
var matchO = text.match(/\x6F/g);
document.write("Found 'o': " + matchO + "<br>");
// \x20 = space (32 in decimal)
var matchSpace = text.match(/\x20/g);
document.write("Found spaces: " + matchSpace.length + "<br>");
</script>
</body>
</html>
Found 'H': H Found 'o': o,o Found spaces: 2
Practical Use Case
Hexadecimal escape sequences are particularly useful for matching special characters or characters that are difficult to type directly:
<html>
<head>
<title>Special Characters with Hex</title>
</head>
<body>
<script>
var data = "Tab separated values";
// \x09 represents tab character
var tabPattern = /\x09/g;
var tabs = data.match(tabPattern);
document.write("Number of tabs found: " + tabs.length + "<br>");
document.write("Original: " + data + "<br>");
document.write("Replaced: " + data.replace(tabPattern, " | "));
</script>
</body>
</html>
Number of tabs found: 2 Original: Tab separated values Replaced: Tab | separated | values
Conclusion
The \x escape sequence in JavaScript RegExp allows you to match characters using their hexadecimal values. This is especially useful for special characters, control characters, or when you need precise character matching based on ASCII/Unicode values.
