Clean up ElasticSearch index names with Regex

A small example what to remove to create a clean index name in JavaScript + RegeX:

var tempIndexName = indexName.replace(new RegExp(‘ ‘, ‘g’), ”);
// Replace thse => #, \, /, *, ?, “, <, >, |
tempIndexName = tempIndexName.replace(/[[\]]/gi, ”);
tempIndexName = tempIndexName.replace(/[$]/gi, ”);
tempIndexName = tempIndexName.replace(/[^\w\s]/gi, ‘-‘);

Advertisements