Is there a way to use RegEx with the Javascript includes() function?


var locations = [
  'New York'

var results = locations.filter(function(location) {
  return location.includes('e');


What I am trying to do with the code above is return a new array of locations that contain the letter 'e', the only problem is if I do

return location.includes('epr')

it doesn't return anything when it should return 'Singapore'. I know I am able to achieve this with regular expressions but how can I make it work with the includes() function? Also, is there a better way at handling what I am trying to achieve?


var locations = [
  'New York'

function getElm(setArray,chkLetter){
  var arr=[];
  for(var i=0;i<setArray.length;i++){
  return arr;


Sorry, but I'm not understanding. Since you know it can be done testing against a pattern, why would you want to use what tests against a string and want that to work as though it were testing against a pattern?

I guess you could use a series of if loops to test for individual characters, but it doesn't make sense to me to not use what is available and known to work.


You can do it like this:

const locations = [
  'New York',

function contains(a, b) {
  return b.split('').every(chr => a.includes(chr));

locations.filter(loc => contains(loc, 'ep'));
// ["Philippines", "Singapore"]

locations.filter(loc => contains(loc, 'epr'));
// ["Singapore"]

@liontas76: if I understand the OP correctly, that won't work:

// []


awesome, thanks!

return location.includes('e') && location.includes('p') && location.includes('r');


My question mostly would be 'why'. Generally speaking if you're going to search a string, its for a substring, rather than "it has all these letters".

If I type in "ore", i'm probably looking for Singapore, not New York.

(Yes, this is a Jurassic Park moment. "Yeah, yeah, but your scientists were so preoccupied with whether or not they could that they didn't stop to think if they should. ")


It's actually for an autocomplete feature I'm trying to do with React, I know there may be plugins I can use out there for this but this is actually how I learn. I don't just use plugins especially if I don't at least understand the basic structures surrounding it. Also, people tend to mistype more often than not so it gives them a better user experience and is also good for business when you give them other options rather than just the one they were searching.


My response to that would be 'search for a substring first'. Maybe if I type 'ore' I did mean new york. But it's far more likely i'm looking for Oregon. If you don't sort your search results...
["Abercrombie", "Absarokee", "Air Force Academy", "Airport Drive", "Airport Heights", "Alberton", "Albertson", "Alcan Border", "Alderpoint", "Alderson", "Alderton", "Alderwood", "Alderwood Manor", "Alfordsville", "Allenport", "Allensworth", "Allerton", "Alpine Northeast", "Alpine Northwest", "Alsace Manor", "Altamonte Springs", "Ambrose", "American Canyon", "American Fork", "Amesbury Town", "Amherst Junction", "Amoret", "Anacortes", "Anchorage", "Anderson", "Anderson Island", "Andersonville", "Andover", "Anmoore", "Annetta North", "Apple Grove", "Arapahoe", "Arboles", "Arbon Valley", "Archer Lodge", "Arden on the Severn", "Ardencroft", "Ardentown", "Ardmore", "Arecibo", "Argonne", "Aristocrat Ranchettes", "Arizona Village", "Arkoe", "Arlington Heights", "Arnold Line", "Arnoldsville", "Arrowhead Springs", "Arroyo Colorado Estates", "Arroyo Gardens", "Arroyo Grande", "Arroyo Seco", "Artondale", "Ash Grove", "Asharoken", "Asheboro", "Asherton", "Ashmore", "Aspermont", "Atascadero", "Atherton", "Atmore", "Attleboro", "Au Sable Forks", "Aurora Center", "Avonmore"....

(This list generated from the 750 city name records contained here: ... and that was just the A's.)


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.