I am most interested in learning more about that piece at the end, '\\$&'
I'm not really sure what its doing, or how it works, but it gets the job done.
The code that I have:
function escapeRegExp(s) {
return s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
}
const searchRegex = new RegExp(
searchQuery
.split(/\s+/g)
.map(s => s.trim())
.filter(s => !!s)
.map(word => `(?=.*\\b${escapeRegExp(word)})`).join('') + '.+',
'i'
)