Home > Blockchain >  Apply regular expression with different string matched
Apply regular expression with different string matched

Time:02-05

I want to use different scenarios of the string matched and filter using Regex.

Ex:-

a = ['Rubico Fans Draw', 'David Rubico Fanch', 'Nikas Fans Farius']

I want out like this:-

'Rubico' 
o/p:- ['Rubico Fans Draw', 'David Rubico Richad']



'Fan'  
o/p:- ['Rubico Fans Draw', 'David Rubico Fanch', 'Nikas Fans Farius']



'Rubico Fans Draw' 
o/p:- ['Rubico Fans Draw']

CodePudding user response:

The solution below does not use regex as you mentioned, but it may be worth checking for your use case:

a.filter(item => item.includes('Rubico'));

CodePudding user response:

const items = ['Rubico Fans Draw', 'David Rubico Fanch', 'Nikas Fans Farius'];

const regex = /Rubico/;

const matchedItems = items.filter(item => item.search(regex) !== -1);

console.log(matchedItems);

  •  Tags:  
  • Related