It appears that JavaScript lacks support for this feature, among other limitations in the language regarding RegExp. However, there exists a library known as XRegExp which includes a unicode addon. This addon allows for unicode support using the \p{}
category definition. For instance, by utilizing \p{Nd}
instead of \d
, you can match digits:
<script src="xregexp-all.js" type="text/javascript"></script>
<script type="text/javascript">
var englishDigits = '123123';
var nonEnglishDigits = '۱۲۳۱۲۳';
var digitsPattern = XRegExp('\\p{Nd}+');
if (digitsPattern.test(nonEnglishDigits)) {
alert('Non-english using xregexp');
}
if (digitsPattern.test(englishDigits)) {
alert('English using xregexp');
}
</script>
UPDATE:
Replaced \p{N}
with \p{Nd}
as it seems that \d
is similar to \p{Nd}
in non-ECMA Script Regex engines. Credit goes to Shervin for bringing this to light. Check out this fiddle by Shervin as well.