$.fn.dataTable.util.escapeRegex()

Since: DataTables 1.10.4

Escape special characters in a regular expression string.

Description

When working with regular expressions it can often be useful to escape input so formatted strings with characters that have special meaning in a regular expression will simply perform a character match. There are a number of special characters in Javascript's regular expressions and DataTables requires the ability to escape these strings internally (for user input of search data) - this method exposes that ability externally.

This is a utility method that is provided for use by extension and plug-in authors. Its use does not directly effect a DataTable or DataTables configuration. It is used internally by DataTables and is made available in the public API to help promote code reuse for extension authors.

Type

function escapeRegex( str )

Description:

Escape special characters in a regular expression string.

Returns:

Escaped string

Example

Perform an escape match search using select elements:

var table = $('#example').DataTable();

table.columns().indexes().flatten().each( function ( i ) {
	var column = table.column( i );
	var select = $('<select><option value=""></option></select>')
		.appendTo( $(column.footer()).empty() )
		.on( 'change', function () {
			// Escape the expression so we can perform a regex match
			var val = $.fn.dataTable.util.escapeRegex(
				$(this).val()
			);

			column
				.search( val ? '^'+val+'$' : '', true, false )
				.draw();
		} );

	column.data().unique().sort().each( function ( d, j ) {
		select.append( '<option value="'+d+'">'+d+'</option>' )
	} );
} );

Related

The following options are directly related and may also be useful in your application development.