Skip to content

Instantly share code, notes, and snippets.

@joeypy
Created October 5, 2022 18:36
Show Gist options
  • Save joeypy/8883c80d75cccc6400984b3cdbd80153 to your computer and use it in GitHub Desktop.
Save joeypy/8883c80d75cccc6400984b3cdbd80153 to your computer and use it in GitHub Desktop.
Advanced searching for JS with regex
// Code to make match with any character present in the input o query in this case
const values = ['Belgium', 'Brest', 'Britian']
const query = 'Be'
// /.*b.*e.*/
const re = RegExp(`.*${query.toLowerCase().split('').join('.*')}.*`)
// [ 'Belgium', 'Brest' ]
const matches = values.filter(v => v.toLowerCase().match(re))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment