- <div id="content"> it's not really simple. It's really simple in every other language. It's ridiculously complex in JS, where you have in and of that can both be used and do different things. Then you also have forEach and the ugly and annoying index based looping. Every other modern language makes looping over a collection easy and straightforward with no surprises or confusion. JS could, too, but it doesn't!</div>
- <script>
- function keywordconvert(str, p1, offset, s) {
- return "<a href=\"link.php?keyword="+encodeURIComponent(p1)+"\">"+p1+"</a>";
- }
- function insertLink(keyword) {
- var content = document.getElementById("content");
- var re = new RegExp("("+keyword+")","ig");
- content.innerHTML = content.innerHTML.replace(re, keywordconvert);
- }
- var myStringArray = ["language","complex", "Collection"];
- var arrayLength = myStringArray.length;
- for (var i = 0; i < arrayLength; i++) {
- insertLink(myStringArray[i]);
- }
- </script>
- //adapted from http://jsfiddle.net/JHHzU/2/