Question

I developed a javascript function to clean a range of Unicode characters. For example, "ñeóñú a1.txt" => "neonu a1.txt". For this, I used a regular expression:

 var = new RegExp patternA ("[\\u0300-\\u036F]", "g");
 name = name.replace (patternA,'');

But it does not work properly in IE. If my research is correct, IE does not detect Unicode in the same way. I'm trying to make an equivalent function using the library XRegExp (http://xregexp.com/), which is compatible with all browsers, but I don't know how to write the Unicode pattern so XRegExp works in IE.

One of the failed attemps:

    XRegExp.replace(name,'\\u0300-\\u036F','');

How can I build this pattern?

Was it helpful?

Solution

The value provided as the XRegExp.replace method's second argument should be a regular expression object, not a string. The regex can be built by the XRegExp or the native RegExp constructor. Thus, the following two lines are equivalent:

name = name.replace(/[\u0300-\u036F]/g, '');
// Is equivalent to:
name = XRegExp.replace(name, /[\u0300-\u036F]/g, '');

The following line you wrote, however, is not valid:

var = new RegExp patternA ("[\\u0300-\\u036F]", "g");

Instead, it should be:

var patternA = new RegExp ("[\\u0300-\\u036F]", "g");

I don't know if that is the source of your problem, but perhaps. For the record, IE's Unicode support is as good or better than other browsers.

XRegExp can let you identify your block by name, rather than using magic numbers. XRegExp('[\\u0300-\\u036F]') and XRegExp('\\p{InCombiningDiacriticalMarks}') are exactly equivalent. However, the marks in that block are a small subset of all combining marks. You might actually want to match something like XRegExp('\\p{M}'). However, note that simply removing marks like you're doing is not a safe way to remove diacritics. Generally, what you're trying to do is a bad idea and should be avoided, since it will often lead to wrong or unintelligible results.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top