替换以给定字符开头的字符串

时间:2021-02-22 01:59:42

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"/>

例如用替换以给定字符开头的字符串替换\ ue40a并用替换以给定字符开头的字符串替换\ ue53c

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 ?

如何在给定的文本中找到以\ ue开头并获取三个下一个字符然后用img标签替换它们的每个字符串?

Thank you for your help !

感谢您的帮助 !

1 个解决方案

#1


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;
    }
});

#1


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;
    }
});