2016-06-21 19 views
1

Ich muss Text wie ;) oder :p durch Emoticon ersetzen, aber ich kann keine Regex erstellen, um dies zu erkennen. Jetzt kann ich nur erkennen, wie :wink:Javascript Regex ersetzen Text mit Emoticons

function replaceEmoticons(text) { 
    var emots = { 
    ";)": "wink", 
    ":)": "xxx", 
    ":p": "xxx", 

    }; 

    return text.replace(/:(.*?):/g, function (match) { 
    return typeof emots[match] != 'undefined' ? 
      '<img src="http://localhost:8080/'+emots[match]+'.png"/>' : 
      match; 
    }); 
} 

Was die gute Regex für das ist?

+0

Warum benötigen Sie einen regulären Ausdruck in der Firstplace? Tun Sie einfach 'yourString.replaceAll (": wink: ", winkSource)' –

+0

Sorry, ich bearbeite meinen Beitrag Ich muss alle Array-Beispiel ersetzen ';)' von wink.png oder ':)' von smile.png – Miky

+0

@ Bálint I bin ziemlich sicher, es gibt keine "replaceAll" auf String-Prototyp ... – ndugger

Antwort

0

Diese Funktion akzeptiert eine Zeichenfolge und gibt eine Zeichenfolge mit allen Ersetzungen innerhalb des Objekts emots zurück.

function replaceText(text) { 
    var emots = { 
    ";)": "wink", 
    ":)": "xxx", 
    ":p": "xxx", 
    }; 

    for(var key in emots){ 
    if(emots.hasOwnProperty(key)){ 
     text = text.replace(new RegExp(escapeRegExp(key), 'g'), '<img src="http://localhost:8080/' + emots[key] + '.png"/>'); 
    } 
    } 
    return text; 
} 

function escapeRegExp(str) { 
    return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); 
} 
2

Versuchen Sie Folgendes. Sie sollten jedoch die Sonderzeichen (und) in Ihren Smileys entkommen, wenn Sie die Regexes machen.

// Hilfsfunktion Sonderzeichen in regulären Ausdruck zu entkommen

function RegExpEscape(text) { 
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); 
} 

function replaceEmoticons(text) { 
    var emoticons = { 
     ':)'   : 'smile.gif', 
     ':('   : 'sad.gif', 
     ';)'   : 'wink.gif' 


    } 

    var result = text; 
    var emotcode; 
    var regex; 

    for (emotcode in emoticons) 
    { 
     regex = new RegExp(RegExpEscape(emotcode), 'gi'); 
     result = result.replace(regex, function(match) { 
      var pic = emots[match.toLowerCase()]; 

      if (pic != undefined) { 
       return '<img src="' + pic + '"/>'; 
        } else { 
       return match; 
        } 
       }); 
    } 

    return result;  
} 
0

I AK1's answer modifiziert und ein Beispiel zur Verfügung gestellt.

// Official Twitch robot emotes: https://twitchemotes.com 
 
var emoticons = { 
 
    ':)' : 'ebf60cd72f7aa600', 
 
    ':(' : 'd570c4b3b8d8fc4d', 
 
    ':o' : 'ae4e17f5b9624e2f', 
 
    ':z' : 'b9cbb6884788aa62', 
 
    'B)' : '2cde79cfe74c6169', 
 
    ':\\' : '374120835234cb29', 
 
    ';)' : '3407bf911ad2fd4a', 
 
    ';p' : '3407bf911ad2fd4a', 
 
    ':p' : 'e838e5e34d9f240c', 
 
    'R)' : '0536d670860bf733', 
 
    'o_O' : '8e128fa8dc1de29c', 
 
    ':D' : '9f2ac5d4b53913d7', 
 
    '>(' : 'd31223e81104544a', 
 
    '<3' : '577ade91d46d7edc' 
 
} 
 
// Convert the emoticon map entries into their fully-qualified paths. 
 
mapIdsToPaths(emoticons, 
 
       'https://static-cdn.jtvnw.net/jtv_user_pictures/', 
 
       'chansub-global-emoticon-', 
 
       '24x18'); 
 

 
// Replace all possible emotes in each paragraph. 
 
document.querySelectorAll('p').forEach(e => e.innerHTML = replaceEmoticons(e.innerHTML, emoticons)); 
 

 
function replaceEmoticons(text, emotes) { 
 
    return Object.keys(emotes).reduce((result, emote) => { 
 
     return result.replace(new RegExp(RegExpEscape(emote), 'gi'), function(match) { 
 
     return (img => img != null ? '<img src="' + img + '"/>' : match)(emotes[match]); 
 
     }); 
 
    }, text); 
 
} 
 

 
// helper function to escape special characters in regex 
 
function RegExpEscape(text) { 
 
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); 
 
} 
 

 
// Map emote ids to their URLs. 
 
function mapIdsToPaths(emotes, url, prefix, size) { 
 
    Object.keys(emotes).forEach((id) => { 
 
    emotes[id] = url + prefix + emotes[id] + '-' + size + '.png'; 
 
    }); 
 
}
<p>Hello ;)<p> 
 
<p>How are you :)?<p> 
 
<p>o_O Today was not a good day... :(<p>