I have an object with strings in it.
filteredStrings = {search:\'1234\', select:\'1245\'}
I want to return
\'124\'
<
Convert one of the strings (search
) to a RegExp character set. Use the RegExp with String#match on the other string (select
).
Note: Unlike lodash's intersection, the result characters are not unique, so for example 4 can appear twice.
var filteredStrings = {search:'1234', select:'124561234'}
var result = (filteredStrings.select.match(new RegExp('[' + filteredStrings.search + ']', 'g')) || []).join('');
console.log(result);