1

I'm writing a function that slices up chunks of text into 1000 character blocks. Before it does that however, I'd like to remove all instances of the string '[edit]'. For some reason, the code below is not doing it for me. What am I doing wrong? (for the whole code string, http://jsfiddle.net/ayoformayo/yTcRb/13/)

function cutUp(){
    var PAname= prompt("Determine PA type and PA Name");
var chunks = [];
var OGstring = document.getElementById('PATypes').value;
var my_long_string = OGstring.replace("[edit]","");


var i = 0;
var n = 0;
while(n < my_long_string.length) {
chunks.push(my_long_string.slice(n, n += 1000));

}
document.getElementById('PAType=Name=Value4').innerHTML = PAname + chunks[0];
document.getElementById('PAType=Name=Value5').innerHTML = PAname + chunks[1];
James Carny
  • 51
  • 2
  • 9
  • 1
    possible duplicate of [Javascript multiple replace](http://stackoverflow.com/questions/832257/javascript-multiple-replace) – Rob W Jul 26 '12 at 21:50
  • Either `var my_long_string = OGstring.replace(/\[edit\]/g,'');` or `var my_long_string = OGstring.split('[edit]').join('');`. – Rob W Jul 26 '12 at 21:51

1 Answers1

2

By default String.prototype.replace will only match a single instance of your searched string. To replace all occurrences you need to pass it the global flag.

myString.replace(/\[edit\]/g,"")

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace

travis
  • 8,055
  • 1
  • 17
  • 19