Django front-end js makes the URL contained in a piece of text accessible

This function is quite necessary. Let's talk about the implementation process and use regular expressions.

1. The original html part of the code:

< h4 id ="softadd" > Link: https://pan.baidu.com/s/11RyCwPnfXnuaZrOIDg8H0Q Password: 6666 </ h4 >

Effect picture:

2. Part of the code after adding the js code:

        <h4 id="softadd">链接: https://pan.baidu.com/s/11RyCwPnfXnuaZrOIDg8H0Q 密码: 6666</h4>
    </div>
</div>


<script>
    if($("#softadd").length){
        var textR=$("#softadd").html();
        var reg= /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
        textR = textR.replace(reg, "<a href='$1$2'>$1$2</a>");
        document.getElementById ( ' softadd ' ) .innerHTML = textR;
    }
</script>

Effect picture:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324981792&siteId=291194637