Intersection of characters in two strings

前端 未结 1 1558
半阙折子戏
半阙折子戏 2020-12-19 20:07

I have an object with strings in it.

filteredStrings = {search:\'1234\', select:\'1245\'}

I want to return

\'124\'
<         


        
相关标签:
1条回答
  • 2020-12-19 20:39

    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);

    0 讨论(0)
提交回复
热议问题