Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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];
share|improve this question
1  
possible duplicate of 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
add comment

1 Answer

up vote 2 down vote accepted

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.