最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to filter exact words from a string? - Stack Overflow

programmeradmin6浏览0评论

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

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.

Share Improve this question asked Jun 19, 2021 at 15:21 Checked rsChecked rs 912 silver badges7 bronze badges 1
  • there is a method Array.every works like Array.some but every condition needs to be satisfied – Krzysztof Krzeszewski Commented Jun 19, 2021 at 15:25
Add a ment  | 

4 Answers 4

Reset to default 2

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

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

.split() each string into an array of lower cased strings (delimiter is a space or a ma). 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, '$'));

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

发布评论

评论列表(0)

  1. 暂无评论