Automatically convert keywords into links using jQuery

We can easily provide the links for specific keywords in a web page by using jQuery automatically as shown below.

<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.4.min.js"></script>

<script type="text/javascript">

        var span = $('div');

        span.html(replaceURLWithHTMLLinks(span.html()));

 

        function replaceURLWithHTMLLinks(text) {

            var exp = /(microsoft)/ig;

            return text.replace(exp, "<a href='https://www.microsoft.com/' target='_blank'>$1</a>");

        }

</script>

Here we are providing the link https://www.microsoft.com/ for keyword Microsoft. So within the div element, each and every occurrence of Microsoft word will get replaced with the hyper link.

The complete code is as shown below.

<html>

<head>

    <title>Automatically convert keywords into links by using jQuery</title>  

</head>

<body>

    <div>

        This site contains a lot of articles for Microsoft Visual Studio 2017, C#.

    </div> 

    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.4.min.js"></script>

    <script type="text/javascript">

        var span = $('div');

        span.html(replaceURLWithHTMLLinks(span.html()));

        //alert(span.html());

        function replaceURLWithHTMLLinks(text) {

            var exp = /(microsoft)/ig;

            return text.replace(exp, "<a href='https://www.microsoft.com/' target='_blank'>$1</a>");

        }

    </script>

</body>

</html>

Output: