/    Sign up×
Community /Pin to ProfileBookmark

Infinite loop when replacing ampersand

I have a function that takes the input from a text area, searches through the text, and should replace &, “, <, or > with &amp;, &lt;, etc.

As the code is now, it will replace other characters, but runs into an infinite loop when dealing with ampersands. I’m thinking it’s finding the ampersands from other things it has already replaced and trying to replace them over and over.

Does anyone have any suggestions how to improve this and break out of the loop?

[CODE]<script language=”javascript”>
function replace(){

//create array containing the four regular expressions
var re = new Array(“&”, “>”, “””, “<“);

//creates an array with the final values
var altChar = new Array(“&amp;”, “&gt;”, “&quot;”, “&lt;”);

//assign the user input to a variable
var userString=document.input.userInput.value;

//runs the search and replace for each symbol, based on it’s array position
for(var i=0; i<4; i++){
//searches the string for the symbols in each array slot
var matchStringIndex = userString.indexOf(re[i]);

//When the indexOf returns a -1, it means the search expression was not found. This loop runs while it is not equal to -1
//or, when the expression is still present in the string
while(matchStringIndex != -1){

userString = userString.replace(re[i], altChar[i]);

//runs indexOf again to see if the while loop needs to run again
matchStringIndex = userString.indexOf(re[i], (matchStringIndex+1));
}
//outputs the transformed value
document.input.userOutput.value=userString;
}
}

</script>[/CODE]

Here’s a link to try it out, but the loop is infinite if you search for an & and may crash your browser. Thanks everyone!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991May 03.2010 — That's because any ampersand is replaced by &amp;, which is replaced by &amp;amp; etc.

Firstly, this is much easier with regular expressions. Secondly, it's probably easiest to replace with !!AMP!!amp;, and then replace all !!AMP!! with &, or something like that.<i>
</i>function replace() {
var re = [/&amp;/g, /&gt;/g, /"/g, /&lt;/g);
var altChar = ["!!AMP!!amp;", "!!AMP!!gt;", "!!AMP!!quot;", "!!AMP!!lt;"];
var userString=document.input.userInput.value;
for(var i=0; i&lt;[B][COLOR="Red"]re.length[/COLOR][/B]; i++) { [B]// Don't hard code in variables like that[/B]
userString = userString.replace(re[i], altChar[i]);
}
document.input.userOutput.value=userString.replace(/!!AMP!!/g,"&amp;");
}
Copy linkTweet thisAlerts:
@drichieauthorMay 03.2010 — That helped a lot. Thanks for your help!
×

Success!

Help @drichie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...