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("[", "<span class='chord'>");
var n2 = n.replaceAll("]", "</span>");
var t = title.replaceAll('{t:', '<h1>');
var t2 = t.replaceAll('}', '</h1>');
var a = artist.replaceAll('{st:', '</h2>');
var a2 = a.replaceAll('}', '</h2>');
var results = document.getElementById('results');
results.innerHTML = t2 + a2 + n2;
}