How to add a variable to regex .match()

Viewed 21

I am trying to make a simple search function where from an array of elements, elements that match given regex are pushed to an array.

However, I am not successful with making that match function contain a variable, not a hardcoded string, tried template literals, it's not working.

let value = $(this).val();
        let foundSchools = [];

        schools.each(function() {
            let school = $(this).val();
            if (school.match(/value.*/)) {
                foundSchools.push(school);
        }
})
0 Answers
Related