开发者

Javascript regexp for smile icon replacements

Could someone help with regexp for replacement of :) for <img src="etc"开发者_运维技巧/>?

in all these cases it shouldn't be replaced:

  • :Dtest test :Dtest - from begining of word or start
  • test:Dtest - between text

in all other cases it should be replaced by <img src="etc"/>

Can't figure out proper pattern.

Thanks ;)


How about

string.replace(/:D(?=\s|$)/g, '<img src="etc"/>')

I tried it here and it seems to work, if I understand your criteria.


"bla :D bla:D".replace(/([^\w]|^)(:D)([^\w]|$)/g, '$1<img src="jeej.png"/>$3')


Your example (looking for just the ":D" smiley) could be solved with this:

var newStr = oldStr.replace(/([\W\b^]):-?D\b/g, '$1<img src="etc"/>');

Don't forget that you'll need other patterns if you want to cover things like 8) :( :P :), etc.

If you do something like

/([\W\b^])([\:8B])-?([DP\(\)\*])(?=[\W\b$])/g

or whatever other permutations of smilies you want to allow, you'll have the eyes and mouth captured in match groups 2 and 3, so you can decide which image to substitute. Or you could use string manipulation for this instead, which is probably what I'd do.


I'd go with

"text :D text :D.".replace(/\b:D\b/g, '<img src="etc" />');

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜