Accent neutralise

Replace accented characters with unaccented counterparts

  • Author: Allan Jardine

When searching a table with accented characters, it can be frustrating to have an input such as Zurich not match Zürich in the table (u !== ü). This type based search plug-in replaces the built-in string formatter in DataTables with a function that will replace the accented characters with their unaccented counterparts for fast and easy filtering.

Note that this plug-in uses the Javascript I18n API that was introduced in ES6. For older browser's this plug-in will have no effect.

Plug-in code

(function(){

function removeAccents ( data ) {
    if ( data.normalize ) {
        // Use I18n API if avaiable to split characters and accents, then remove
        // the accents wholesale. Note that we use the original data as well as
        // the new to allow for searching of either form.
        return data +' '+ data
            .normalize('NFD')
            .replace(/[\u0300-\u036f]/g, '');
    }

    return data;
}

var searchType = jQuery.fn.DataTable.ext.type.search;

searchType.string = function ( data ) {
    return ! data ?
        '' :
        typeof data === 'string' ?
            removeAccents( data ) :
            data;
};

searchType.html = function ( data ) {
    return ! data ?
        '' :
        typeof data === 'string' ?
            removeAccents( data.replace( /<.*?>/g, '' ) ) :
            data;
};

}());

CDN

This plug-in is available on the DataTables CDN:

JS

Note that if you are using multiple plug-ins, it is beneficial in terms of performance to combine the plug-ins into a single file and host it on your own server, rather than making multiple requests to the DataTables CDN.

Version control

If you have any ideas for how this plug-in can be improved, or spot anything that is in error, it is available on GitHub and pull requests are very welcome!

Example

$(document).ready(function() {
      $('#example').dataTable();
  } );