Search code examples
javascriptnode.jsnode-mysql

Updating banned word string from query to match


I'm trying to query from the database words at set interval and match them against Twitter API, I can't figure out how to the the variable

var bannedName = rows[0].name;

into this expression, right now just placing bannedName variable would result undentified

if(!tweetTextRetweet.match(/^RT|@TopU3DAssets|@LatestAssetBot|@IndieLeverage|@Parodossy|@GameArtSleuth|@Alex_TNT/)) 

to result like

if(!tweetTextRetweet.match(bannedName))

In order to have it work and match against the word. If I put the query outside interval It does not update the query leaving the result unmodified when changed in database

Here's what I currently have

setInterval(function() {

    con.query("SELECT setting,name FROM droid_settings WHERE ID = 2", function(error,rows){ 
        var bannedName = rows[0].name;
        console.log('INFO ------- ',bannedName);

    });

    for (var i = 0; i < tweets.length; i++) {
        var tweetTextRetweet = tweets[i].text; 

        if (!tweetTextRetweet.match(/^RT|@TopU3DAssets|@LatestAssetBot|@IndieLeverage|@Parodossy|@GameArtSleuth|@Alex_TNT/)) { // search for RT and @
            console.log('INFO -------',tweets[i].id);
            dateTime(); 
            console.log('INFO -------',tweets[i].text);


            tweets.length = 0;
        }else{
            //console.log('SKIPPED');
            //dateTime(); 
        }

    }
    // reset the tweets array
    tweets.length = 0;
},  4 * 1000);

Solution

  • One straighforward way is to just add a second condition to your if statement:

    if (tweetTextRetweet.indexOf(bannedName) !== -1 && !tweetTextRetweet.match(/^RT|@TopU3DAssets|@LatestAssetBot|@IndieLeverage|@Parodossy|@GameArtSleuth|@Alex_TNT/)) {
    

    You can also put the bannedName into the regex, but you have to construct the regex from a string with the regex constructor to do so and you have to make sure that bannedName does not contain any special regex matching characters that are not properly escaped (that's why I used .indexOf() above because it isn't sensitive to regex characters in bannedName so the name doesn't have to be escaped.

    Assuming bannedName does not contain any special regex characters, the regex could be built dynamically like this:

    var regex = new RegExp(bannedName + "|^RT|@TopU3DAssets|@LatestAssetBot|@IndieLeverage|@Parodossy|@GameArtSleuth|@Alex_TNT");
    if (!regex.test(tweetTextRetweet)) {
    

    If you need to escape possible regex characters in bannedName, you can use a function from this answer to do so: Is there a RegExp.escape function in Javascript?.


    Maybe I first misunderstood your question. If you want to use the bannedName from the query, then just put the rest of your code inside the function where bannedName is available.

    setInterval(function() {
    
        con.query("SELECT setting,name FROM droid_settings WHERE ID = 2", function(error,rows){ 
            var bannedName = rows[0].name;
            console.log('INFO ------- ',bannedName);
    
            for (var i = 0; i < tweets.length; i++) {
                var tweetTextRetweet = tweets[i].text; 
    
                if (tweetTextRetweet.indexOf(bannedName) === -1 && !tweetTextRetweet.match(/^RT|@TopU3DAssets|@LatestAssetBot|@IndieLeverage|@Parodossy|@GameArtSleuth|@Alex_TNT/)) { // search for RT and @
                    console.log('INFO -------',tweets[i].id);
                    dateTime(); 
                    console.log('INFO -------',tweets[i].text);
    
    
                    tweets.length = 0;
                }else{
                    //console.log('SKIPPED');
                    //dateTime(); 
                }
    
            }
            // reset the tweets array
            tweets.length = 0;
        });
    
    },  4 * 1000);