I am trying to split a string based on the delimter \$
. I have tried this unsuccessfully.
The code that I have is at https://js.do/sun21170/77657, which is also pasted below.
Question: What am I doing wrong in this example when splitting by \$
?
var trickyString = "sd sewq wee r r ttttt $300 rrtrt utu iwiwi \$500 kjgf ihj \$215 ghi";
//document.getElementById("div0").innerHTML = trickyString;
function splitString() {
//Why is splitting by \$ not giving 3 elements but is instead giving 4 elements?
var array1 = trickyString.split(/\$/);
document.getElementById("div1").innerHTML = "<b>Length = " + array1.length + "</b>";
for (var i = 0; i < array1.length; i++) {
document.getElementById("div1").innerHTML += "<br>" + array1[i];
}
var array2 = trickyString.split("$");
document.getElementById("div2").innerHTML = "<b>Length = " + array2.length + "</b>";
for (var j = 0; j < array1.length; j++) {
document.getElementById("div2").innerHTML += "<br>" + array2[j];
}
}
<button onclick="splitString();return false;">Split a tricky string</button>
<h4>Tricky string</h4>
<div id="div0" style="color:green">sd sewq wee r r ttttt $300 rrtrt utu iwiwi \$500 kjgf ihj \$215 ghi</div>
<h4>Split using \$ as delimiter</h4>
<div id="div1" style="color:red"></div>
<h4>Split using $ as delimiter</h4>
<div id="div2" style="color:blue"></div>