Internationalization and localization tools


Locale-Sensitive JavaScript Constructor

/pattern/flags;
RegExp(pattern);
RegExp(pattern, flags);

Internationalization (I18n) Method Overview

A RegExp object can be constructed using literal notation or by calling the constructor, passing in the pattern and flags. RegExp only works with the basic Latin characters (i.e. English characters).

The following example searches for words in the string by applying a regular expression, using the two forms of construction:

var str = "This is a sentence.";
var resultArray1 = str.match(/\w+/g);
var resultArray2 = str.match(new RegExp("\w+", "g"));


Both match methods return the same array with 4 strings:

resultArray: This,is,a,sentence

Click here (w3schools) and here (MDN) for additional details.

I18n Issues

Whether or not calling RegExp is an i18n issue is dependent on how it is being used in the application. If the regular expression is to be applied to a UI or user-entered string that might contain non-English characters, then the RegExp may be problematic.

Suggested Replacement

Use the RegExp method when strings that the regular expression will be applied to are programmatic strings that do not require translation.

If the string is to be displayed to the user, or is entered by the user, and you want to use \w to search for characters, then you will need to use a 3rd party regular expression object that supports all Unicode characters (for example, XRegExp).

Applying regular expressions to find words may need to be refactored, since some locales do not separate words with spaces.


Globalyzer will detect this method and report it as an I18n issue. If you have determined that the call is being handled correctly, you can use Globalyzer's Ignore Comment functionality to ensure that it isn't picked up in a subsequent scan.



Locale-Sensitive JavaScript Methods

 

Lingoport internationalization and localization services and software