1

I am currently developing a program which filters words based off of a txt file that has words inserted into it separated by commas.

The problem I am having is when I try putting regular expressions inside my txt file jQuery treats it as a string ignoring the regular expression entirely.

How can I make it so that I can put words inside my txt file in a reg expression format so that I only have the write a filtered word only once depending on how many different formats a viewer may type it.

If this is not possible can someone point me in the right direction to achieve this function. Any help would be appreciated. Thank you.

Javascript:

 // This grabs the data from the txt file and splits each word my commas
 $.get('word-list.txt', function(data) {
    pbfFilterWords = data.split(', ');
    pbfFilterWords.forEach(function(word){
    pbfWordList = word;
 });

 // This defines a global variable so the filter button has a list of words to filter with
 var pbfWordList = pbfFilterWords;
 //console.log(pbfWordList);

 // Function for filter button
 $('.pbf-link-container[contenteditable]').html;
 $('#pbf-filter').click(function(){
        var $pbfOutput = $('.pbf-link-container[contenteditable]').html();
        // Array of words for filter
        var pbfFilterWords = pbfWordList;
            // Output to new DIV and remove specified keywords from pbfFilterWords
            $('.pbf-link-output').html($pbfOutput);
            // To make pbfFilterWords not case sensitive
            $.expr[":"].contains = $.expr.createPseudo(function(arg) {
                return function( elem ) {
                return $(elem).html().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
                    };
            });
            // Function to output the filtered words
            $.each(pbfFilterWords , function(i , val){
            $('.pbf-link-output > div:contains("'+val+'")').remove();
        });
    });
});

word-list.txt:

'(Jennifer|Jen|Jenny)\b\w+\b' , baby bullet, baby-bullet, 'back2life', 'back-2-life'
Andrew Lee
  • 304
  • 5
  • 14
  • I'm a little confused, how is this a duplicate thread if my question is trying to read regexp from a internal txt file vs the supposed duplicate thread is based on what the user types? I am still new to programming so can someone explain to me how this is a duplicate? – Andrew Lee Aug 24 '17 at 01:17

1 Answers1

0
var re = new RegExp("a|b", "i");
// same as
var re = /a|b/i;

Ref - Converting user input string to regular expression

kawadhiya21
  • 2,458
  • 21
  • 34