replaceAll function

by James Doyle

HTML

<input type="text" placeholder="title" id="title" /><br/>
<input type="text" placeholder="artist" id="artist" /><br/>
<textarea id="str"></textarea><br/>
<button onclick="stripB()">clean string</button><br/>
<div id="results"></div>

CSS

body {
    font-size: 12px;
    margin: 1em;
}
input {
    width: 200px;
    margin: 0 0 10px;
}
textarea{ 
    width: 400px;
    height: 150px;
    font-size: 12px;
}
#results {
    margin: 10px 0;
    padding: 2px;
    border: 1px solid red;
}

JavaScript

String.prototype.replaceAll = function(str1, str2, ignore) {
    return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g, "\\$&"), (ignore ? "gi" : "g")), (typeof(str2) == "string") ? str2.replace(/\$/g, "$$$$") : str2);
}

function stripB() {
    var str = document.getElementById('str'),
        title = document.getElementById('title').value,
        artist = document.getElementById('artist').value;
    str = str.value;
    var n = str.replaceAll("[", "&lt;span class='chord'&gt;");
    var n2 = n.replaceAll("]", "&lt;/span&gt;");
var t = title.replaceAll('{t:', '&lt;h1&gt;');
var t2 = t.replaceAll('}', '&lt;/h1&gt;');
var a = artist.replaceAll('{st:', '&lt;/h2&gt;');
var a2 = a.replaceAll('}', '&lt;/h2&gt;');
    var results = document.getElementById('results');
    results.innerHTML = t2 + a2 + n2;
}