首页 > 解决方案 > 在电子邮件 ID 中查找并替换到 thymleaf 中的 mailto 链接

问题描述

我想知道 thymleaf 模板中的电子邮件 ID 到 mailto 链接,例如:hello@gmail.com 到<a href="mailto:hello@gmail.com"></a>

它应该使用正则表达式来查找电子邮件 ID。

我曾尝试使用 thymleaf 内联 javascript,但不幸的是不能在 Outlook 中工作。

<script>
/*<![CDATA[*/
    function formatEmails(){
        var content=document.getElementById('content').innerHTML;       
        var regex = /\S+@\S+\.\S+/g
        var matches = [];
        var match = regex.exec(content);
        var formatedContent=content;
        while (match != null) {
            if(matches.indexOf(match[0]) === -1)matches.push(match[0]); 
            match = regex.exec(content);
        }
        matches.forEach(function(email,index){
            formatedContent=formatedContent.split(email).join('<a href="mailto:'+email+'">'+email+'</a>')
        });
        document.getElementById('content').innerHTML=formatedContent;
   }
/*]]>*/
</script>

如果没有直接的方法可以做到这一点,如何做到这一点?提前致谢。

标签: thymeleaf

解决方案


推荐阅读