text = "At the heart of GitHub is Git, an open source project started by Linux creator Linus Torvalds. Matthew McCullough, a trainer at GitHub, explains that Git, like other version control systems, manages and stores revisions of projects."; change = function(word) { var r = new RegExp("(" + word + ")", "gi"); document.querySelector("#content").innerHTML = text.replace(r, "<b>$1</b>"); } // notes // text2 = "At the heart of GitHub is Git, an open source project started by Linux creator Linus Torvalds."; // var element = document.querySelector("#content"); // var changeText = function(word) { // var r = new RegExp("(" + word + ")", "gi"); // global match and ignore case flag // element.innerHTML = text2.replace(r, '<a href="#">$1</a>'); // } // changeText('git'); // document.querySelector("#content").innerHTML = document.querySelector("#content").outerHTML.replace(/</g, '<');
<a href="javascript:change('Git');">Use 'Git'</a> <a href="javascript:change('git');">Use 'git'</a> <a href="javascript:change('github');">Use 'github'</a> <a href="javascript:change('pr');">Use 'pr'</a> <p id="content">At the heart of GitHub is Git, an open source project started by Linux creator Linus Torvalds. Matthew McCullough, a trainer at GitHub, explains that Git, like other version control systems, manages and stores revisions of projects.</p>
a { display: inline-block; text-decoration: none; margin: 0 0 4px 0; background: #B0B0B0; border-radius: 4px; color: #FFF; padding: 6px; } a:hover { color: #000; } p { margin: 10px 0 0 0; padding: 0; border-radius: 10px; } p b { background: #000; color: #FFF; font-weight: normal; }