var app = angular.module('demo', ['ngMaterial']); app.run([ '$rootScope', function ($rootScope) { var tokens = _([].slice.apply(document.querySelectorAll('p'))) .map((p) => p.textContent.split(' ')) // Split on words. .flatten() // Flatten. .map((token) => token.replace(/\W/g, '')) // Remove punctuation. .filter((token) => token.match(/\w/)) // Remove empty tokens. .map(_.upperFirst) // Consistent case. .uniq() // Remove duplicates. .value(); $rootScope.search = (query) => tokens.filter( (t) => query ? t.match(new RegExp(query, 'i')) : true); } ]);