Skip to content

Instantly share code, notes, and snippets.

@nfroidure
Last active December 22, 2015 03:58
Show Gist options
  • Save nfroidure/6413396 to your computer and use it in GitHub Desktop.
Save nfroidure/6413396 to your computer and use it in GitHub Desktop.
A regExp with unicode chars
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* ***** END LICENSE BLOCK ***** */
RegExp.createLatinRegExp = (function() {
var LATIN_CHRS = [
'AÃÁÄÂÀ'.split('').join('|'),
'aáäâàãå'.split('').join('|'),
'CÇ'.split('').join('|'),
'cç'.split('').join('|'),
'DÐ'.split('').join('|'),
'dð'.split('').join('|'),
'EÉÈËÊ'.split('').join('|'),
'eéèëê'.split('').join('|'),
'IÏÎÌÍ'.split('').join('|'),
'iïîìí'.split('').join('|'),
'NÑ'.split('').join('|'),
'nñ'.split('').join('|'),
'OÕÖÔÒÓØ'.split('').join('|'),
'oöôòóõø'.split('').join('|'),
'UÜÛÙÚ'.split('').join('|'),
'uüûùú'.split('').join('|'),
'YŸÝ\u00A5'.split('').join('|'),
'yÿý'.split('').join('|'),
['AE','Æ'].join('|'),
['ae','æ'].join('|'),
['OE','ŒŒ\u008C'].join('|'),
['oe','œ\u009C'].join('|'),
['ss','ß'].join('|')
],
LATIN_REGEXP = new RegExp(LATIN_CHRS.join('|'));
return function(pattern, flags) {
return new RegExp(
pattern.replace(LATIN_REGEXP, function(match) {
for(var i=LATIN_CHRS.length-1; i>=0; i--) {
if(-1!==LATIN_CHRS[i].indexOf(match)) {
return '(?:'+LATIN_CHRS[i]+')';
}
}
}) , flags);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment