0

So I've managed to modify a script used to automatically replace text within a specific element based on a simpler dictionary script someone else wrote. But i'm starting to see a build up of redundant code I think could be simplified further, but unsure how.

What I have currently implemented:

var dictionary= {
    " jquery ":" #jQuery ",
    " jQuery ":" #jQuery ", 
    " JQuery ":" #jQuery ",

    " jQuery1 ":" #jQuery ",
    " jQuery2 ":" #jQuery ", 
    " jQuery3 ":" #jQuery ",
};

jQuery(document).ready(function() {
    setTimeout(function() {
$("#status").each( function(){
    for( var ptrn in dictionary){
        $(this).text( $(this).text().replace(new RegExp(ptrn ,"g"), dictionary[ptrn] ) );
    }
});
}, 500);
});

What I would like is to simplify combining 3 words its to detect, without multiples of "#jQuery", something like the following or similar. "jQuery1" would be something it is suppose to detect opposed to "jQuery5":

" jQuery1 "," jQuery2 "," jQuery3 ":" #jQuery ",

This modified jQuery script is based off of this: https://stackoverflow.com/a/9908925/2038928

  • You have 3 repeated object properties `JQuery`, it's not cool – P.S. Sep 29 '17 at 02:15
  • And what should `" jQuery1 "," jQuery2 "," jQuery3 ":" #jQuery ",` be? A string? Object is a key-value pair (one key and one value), there is no way to use `" jQuery1 "," jQuery2 "," jQuery3 ":" #jQuery ",` as object property. – P.S. Sep 29 '17 at 02:17
  • jQuery1, jQuery2 and jQuery3, are used as an example of a specific word used to be detected and replaced with #jQuery. This is useful for specific things to be detected if they do or don't have trailing spaces or symbols and such. But doing so for each one to replace with #jQuery, is un-necessary, if some how 1, 2 and 3, can be combined. – Zach Reynolds Sep 29 '17 at 02:22

3 Answers3

0

The script for replacing text uses regular expressions, so the keys of the dictionary could be regular expressions.

For example, this dictionary sums up the one in your question with only one entry, if you make the regular expression case insensitive:

var dictionary= {
    " jquery\\d? ":" #jQuery "
};

You could also match different strings, specifying each one with a single entry, like this:

var dictionary= {
    "(text1|text2|text3)":" #jQuery "
};

To make the regular expressions case insensitive, change

new RegExp(ptrn, "g")

to:

new RegExp(ptrn, "ig")
ncardeli
  • 3,452
  • 2
  • 22
  • 27
  • `"(text1|text2|text3)":" #jQuery "` represents the idea, but can blank spaces be added after any word such as "text1 ", so its not removed from URL's or "words" such as "abcdtext1abcd"? – Zach Reynolds Sep 29 '17 at 02:29
  • @zachreynolds yes, for example, " (text1|text2|text3) " will match " text1 ", " text2 ", and " text3 " – ncardeli Sep 29 '17 at 02:32
  • but I am seeking it to also include the white space or text such as `(11 22|33 44|55 66)` can it still be used with these expressions? – Zach Reynolds Sep 29 '17 at 02:36
  • @zachreynolds It's ok, it will consider the white spaces. You can test the regular expression here, to see what it will match: regexpal.com – ncardeli Sep 29 '17 at 02:38
  • thank you, this works, my dictionary is large, I wish it could use both `" A":" a",` and `" A |aa":" a",`. This would save me from having to completely redo my dictionary. would `new RegExp(ptrn ,"g","ig")` work? – Zach Reynolds Sep 29 '17 at 02:44
0

You can use RegExp /jquery[\d\s]+/i to match all properties of object at Question

let res = str.replace(/jquery[\d\s]+/i, "#jQuery ");
guest271314
  • 1
  • 15
  • 104
  • 177
0

Taken from user on this threat and modified slightly: https://stackoverflow.com/a/46480860/8589517

$(this).text( $(this).text().replace(new RegExp(ptrn ,"g","ig"), dictionary1[ptrn] ) );

" test":" #test", "(10 20|30 40)":"#jQuery",

Result:

10 20 10 25 30 40 50 test to: #jQuery 10 25 #jQuery 50 #test

Another example:

From: AI is used by many social media platforms for censorship.

With: "(AI | ai | aI |artificial intelligence)":" #ArtificialIntelligence ",

To: #ArtificialIntelligence is used by many social media platforms for censorship.