Stack Overflow Asked by askmeaquestion1234 on November 27, 2021
I have an array of all stock symbols. When a user posts the message 'TSLA will open green tomorrow.'
, I want to detect the word 'TSLA'
. But the problem is my code is also detecting 'T'
, 'S'
, and 'LA'
, since those are all valid stock symbols.
So I created a list of prohibited symbols consisting of every capital letter from A
to Z
. But I don’t know how to apply it.
const url = 'https://dumbstockapi.com/stock?format=tickers-only&exchange=NASDAQ,NYSE,AMEX';
const bodyArray = [];
const blockedList = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
request(url, (error, { statusCode }, body) => {
return error ? console.log(error) : statusCode == 200 ? bodyArray.push(JSON.parse(body)) : 0;
});
client.on('message', msg => {
const { content, author: { tag } } = msg;
const [arr] = bodyArray;
const blocked = arr.indexOf(word => content.toUpperCase().includes(word) > -1);
if (blocked.length) {
console.log(`${tag} used a word in list.`);
return msg.delete().catch(console.error);
}
});
You need to use a RegExp
to match every stock symbol in your list, but use the b
boundary symbol to prevent finding matches inside matches.
const string = 'TSLA will open green tomorrow. LA will too.';
const symbols = ['T', 'S', 'LA', 'TSLA'];
const regExpString = String.raw`b`+ symbols.join(String.raw`b|b`);
const regExp = new RegExp(regExpString, 'gi');
const matches = string.match(regExp);
console.log(matches); // [ 'TSLA', 'LA' ]
Answered by GirkovArpa on November 27, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP