-1

I am trying to find a way to replace some special strings with their relative smileys.

for example replace \ue40a with <img src="image/path"/> and replace \ue53c with <img src="image/path/2"/>

How can I find , in a given text , each string that begins with \ue and get the three next characters and then replace them by an img tag ?

Thank you for your help !

R4mirezZ
  • 39
  • 9

1 Answers1

2

Use replace and a match function:

input = input.replace(/(\\u[a-f0-9]{4})/gi, function(m) {
    switch (m.toUpperCase())
    {
        case "\\UE40A": return '<img src="image/path"/>';
        case "\\UE53C": return '<img src="image/path/2"/>';
        default: return m;
    }
});
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • Try it with `input = '
    '`.
    – Oriol Nov 28 '14 at 17:17
  • If you alert() your *real* input string and actually *see* "\ue40a" then to replicate that string in javascript you need to use two \\ instead of one \ - http://jsfiddle.net/ztj0akp6/ – Alex K. Nov 28 '14 at 17:31