Unicode



Let us state a problem. We want to find if a given text contains the special character ©, which is a copyright sign. Every character has a corresponding unicode. The unicode varies from 0000 to FFFF in hexadecimal. The unicode corresponding to the copyright sign © is 00A9. One obvious solution is to copy paste the copyright sign © in the pattern. This solution is presented in the example below.

<html>
<body>
<script type="text/javascript">
<!--
/*
********************************************************
Javascript Regular Expression Example 26
********************************************************
*/
var pattern1=RegExp("©");
var string1 = new Array(2);
string1[0] = "This sentence contains a copyright sign, © ";
string1[1] = "This sentence does not contains a copyright sign";
var i;
for(i=0; i<=1; i++)
{
if (pattern1.test(string1[i]))
{
document.write(string1[i], " -->matches RegExp(\"©\")","<br\>");
}
else
{
document.write(string1[i], " --> does not match RegExp(\"©\")", "<br\>");
}
}
//-->
</script>
</body>
</html>

Try this Example online


You can try this example online at - here .

If we "run" this code, we get the following output

This sentence contains a copyright sign, © -->matches RegExp("©")
This sentence does not contains a copyright sign --> does not match RegExp("©").


This was expected. There is nothing special in it. However, we could also write the regular expression using the unicode of the copyright sign. The unicode of the copyright sign is 00A9. Here is the same program written with the unicode.


<html>
<body>
<script type="text/javascript">
<!--
/*
********************************************************
Javascript Regular Expression Example 27
Understanding the use of Unicode
********************************************************
*/
var pattern1=RegExp("\u00A9");
var string1 = new Array(2);
string1[0] = "This sentence contains a copyright sign, © ";
string1[1] = "This sentence does not contains a copyright sign";
var i;
for(i=0; i<=1; i++)
{
if (pattern1.test(string1[i]))
{
document.write(string1[i], " -->matches RegExp(\"©\")","<br\>");
}
else
{
document.write(string1[i], " --> does not match RegExp(\"©\")", "<br\>");
}
}
//-->
</script>
</body>
</html>

Try this Example online


You can try this example online at - here .