[英]How to convert emoticons within text with RegEx?
The following regex works in all but one case. 除了一种情况外,以下正则表达式均适用。
If I start a text with ":-)" the emoticon should be converted and it is not. 如果我以“ :-)”开头,则应转换图释,而不是。 What do i have to add to make this possible?
我必须添加什么才能使其成为可能?
// :-O :O :-o :o
_message = _message.replace(/\s:-?[Oo]\s/g, '<i class="icon-emoji-freaked-mini"></i>');
// :-) :)
_message = _message.replace(/\s:-?\)\s/g, '<i class="icon-emoji-smile-mini"></i>');
// :-D :D
_message = _message.replace(/\s:-?D\s/g, '<i class="icon-emoji-laugh-mini"></i>');
// :-( :(
_message = _message.replace(/\s:-?\(\s/g, '<i class="icon-emoji-sad-mini"></i>');
// ;-) ;)
_message = _message.replace(/\s;-?\)\s/g, '<i class="icon-emoji-wink-mini"></i>');
// :-/ :/ :-| :| excluded: :-// :// (URLs)
_message = _message.replace(/\s:-?[\/\|](?!\/)\s/g, '<i class="icon-emoji-well-mini"></i>');
You can use ^
to anchor to the start of the string (It has this meaning when it is outside square brackets). 您可以使用
^
锚定到字符串的开头(当它在方括号之外时,具有此含义)。 So we say <start of string or following a whitespace character>
因此,我们说
<start of string or following a whitespace character>
We can also do similar with $
and anchoring to the end of the string. 我们也可以使用
$
并将其锚定到字符串的末尾。
// :-O :O :-o :o
_message = _message.replace(/(^|\s):-?[Oo](\s|$)/g, '<i class="icon-emoji-freaked-mini"></i>');
// :-) :)
_message = _message.replace(/(^|\s):-?\)(\s|$)/g, '<i class="icon-emoji-smile-mini"></i>');
// :-D :D
_message = _message.replace(/(^|\s):-?D(\s|$)/g, '<i class="icon-emoji-laugh-mini"></i>');
// :-( :(
_message = _message.replace(/(^|\s):-?\((\s|$)/g, '<i class="icon-emoji-sad-mini"></i>');
// ;-) ;)
_message = _message.replace(/(^|\s);-?\)(\s|$)/g, '<i class="icon-emoji-wink-mini"></i>');
// :-/ :/ :-| :| excluded: :-// :// (URLs)
_message = _message.replace(/(^|\s):-?[\/\|](?!\/)(\s|$)/g, '<i class="icon-emoji-well-mini"></i>');
Additionally, as Casimir says, you can do something like this, taking advantage of the fact that as long as it begins with letter, _, or $, a javascript variable may contain almost any character. 此外,就像卡西米尔(Casimir)所说的那样,您可以利用一个事实,即只要以字母,_或$开头的javascript变量几乎可以包含任何字符,就可以这样做。
_message = "He Loves me :), he loves me not. :(";
var Smiles = {"e:-O": "freaked-mini","e:O": "freaked-mini","e:-)": "smile-mini","e:)": "smile-mini","e:-D": "laugh-mini","e:D": "laugh-mini","e:-(": "laugh-mini","e:(": "laugh-mini","e;-)": "wink-mini","e;)": "wink-mini","e:-\\": "well-mini","e:\\": "well-mini","e:-\|": "well-mini","e:\|": "well-mini"}
_message = _message.replace(/(?:^|\s)(:-?[Oo]|:-?\)|:-?\(|:-?D|s:-?|;-?\)|:-?[\/\|](?!\/))(?=\s|$)/g,function (match,p1) {
return '<i class="icon-emoji-' + Smiles["e" + p1] + '></i>';
});
console.log(_message);
One problem I encountered, no matter which code styling is that it will not catch an emoticon followed immediately by a punctuation mark and that makes me sad :(.
. 无论使用哪种代码样式,我都遇到的一个问题是,它不会立即抓住带后缀的标点符号,
that makes me sad :(.
。
There's an easy fix though, we'll change (?=\\s|$)
to (?=\\s|$|\\.|,|\\?|\\!)
. 不过有一个简单的解决方法,我们将
(?=\\s|$)
更改为(?=\\s|$|\\.|,|\\?|\\!)
。 And that makes me happy<i class="icon-emoji-smile-mini></i>.
_message = _message.replace(/(?:^|\s)(:-?[Oo]|:-?\)|:-?\(|:-?D|s:-?|;-?\)|:-?[\/\|](?!\/))(?=\s|$|\.|,|\?|\!)/g,function (match,p1) {
return '<i class="icon-emoji-' + Smiles["e" + p1] + '></i>';
});
console.log(_message);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.