How to filter exact words from a string?

Viewed 2226

My main objective is to check if the string contains array of words. If the string contain $ in front of a word, I don't want it to check the string for array of words and want to straight way console.log it.

The problem I'm having is that its not checking for "Exact words". For example if you put E, the text should show up since, there's no words that contains only 'E' however, its not showing up.

For instance:

const res = `EU, U.S. REACH DEAL TO RESOLVE BOEING-AIRBUS TRADE DISPUTE
$BA`;
const filters = ["E", "OPEC", "Repo"];
if (!filters.some(element => res.includes(element))) {
  console.log(res);
}

Another method, I was thinking maybe using the split method and to check for every array item whether it's a filtered word or not.

4 Answers

Use ES6 Array.filter() and arrow functions with expression body:

var words = ['get', 'help', 'set', 'moon', 'class', 'code', 'Get',  ];

var letter = 'e';

var word = "get";

const output = words.filter(x=>x.includes(letter));
console.log(output);

const output2 = words.filter(x => x.toLowerCase().includes(word));
console.log(output2);

var res = `EU, U.S. REACH DEAL TO RESOLVE BOEING-AIRBUS TRADE DISPUTE $BA`;

var filters = ["E", "OPEC", "Repo"];

if (filters.some(element => new RegExp('\\b'+element + '\\b').test(res))) {
  console.log(res);
}

I think you can get some idea from this example.click here to see jsFiddle

var words = ['get', 'help', 'set', 'moon', 'class', 'code'];

var letter = 'e';

function find(words, letter) {

  letter = letter.split(''); //get it to as  object the  we can use it in every method as follwing

  return words.filter(function(word) { //get pne buy one word in words array

    return letter.every(function(char) {
      return word.includes(char); // return word including the letter that you request
    });
  });
}

const output = find(words, letter);
console.log(output);

.split() each string into an array of lower cased strings (delimiter is a space or a comma). If there's more than one string to be filtered, put them into an array

const strings = ['aaa, BBB, ccc', 'vhko nuu', 'String'];
let arrArr = strings.map(str => {
  return str.toLowerCase().split(/[,\s]/);
});
// arrArr = [['aaa', 'bbb', 'ccc'], ['vhko', 'nuu'], ['string']]

Then run each string array of arrArr (array of arrays) through .flatMap((strArr, idx)... and each string of each strArr (string array) through .flatMap(str...

return arrArr.flatMap((strArr, idx) => {
  return strArr.flatMap(str => {...

For each str (string) use .includes(list) and .startsWith(char) in a ternary to test if str is in the list or if it starts with a specific string (if the third parameter char was passed).

<IF> list.includes(str) ? <TRUE|THEN> [strings[idx]] 
: <ELSE IF> char && str.startsWith(char) ? <TRUE|THEN> [strings[idx]] 
: <ELSE|THEN> []; 

const strs = [
`EU, U.S. REACH DEAL TO RESOLVE BOEING-AIRBUS TRADE DISPUTE $BA`, 
`a, b, C, d, E, f`, 
`OPEC reaches agreement`, 
`This has opec., repo#, and a dollar$ so this should be ignored`
];

const words = ["e", "opec", "repo"];

const wordFilter = (list, strings, char) => {
  let arrArr = strings.map(str => {
    return str.toLowerCase().split(/[,\s]/);
  });
  return arrArr.flatMap((strArr, idx) => {
    return strArr.flatMap(str => {
      return list.includes(str) ? [strings[idx]] : 
      char && str.startsWith(char) ? [strings[idx]] : 
      [];
    });
  });
};

console.log(wordFilter(words, strs, '$'));

Related