avoiding an infinite loop with JavaScript String.replace

Discuss how to use and promote Web standards with the Mozilla Gecko engine.
Post Reply
User avatar
amir_e_a
Posts: 22
Joined: December 9th, 2005, 5:03 am

avoiding an infinite loop with JavaScript String.replace

Post by amir_e_a »

In the Hebrew Wikipedia there's an extension called "Gadget-EditReplace.js". You can see the code here:

* http://he.wikipedia.org/wiki/MediaWiki: ... Replace.js

It essentially uses JavaScript String.replace method to replace strings in an article.

The problem is this: If the source string is included in the target string, the replace method will run infinitely.

For example, consider an article about a person whose last name is Friedmann, but it is accidentally spelled Friedman, with one 'n': "Alex Friedman was a great pianist. Friedman was born in New York." I want to replace all occurences of "Friedman" to "Friedmann". If i use the replace method the method will run infinitely and after a few seconds the browser will crash.

Here's the simplest code:

article = "Alex Friedman was a great pianist. Friedman was born in New York.";
article = article.replace("Friedman", "Friedmann");

Is there a way to replace all the strings, but only once and not in a loop?

I found documentation for the replace method here: https://developer.mozilla.org/en/Core_J ... ng/replace

...but i couldn't find an answer there.

Thanks in advance.
User avatar
BenoitRen
Posts: 5946
Joined: April 11th, 2004, 10:20 am
Location: Belgium

Re: avoiding an infinite loop with JavaScript String.replace

Post by BenoitRen »

Try replacing "Friedman " (with a space), "Friedman." and "Friedman," instead. That should take care of most occurences, depending if colons and semi-colons are also used.
User avatar
amir_e_a
Posts: 22
Joined: December 9th, 2005, 5:03 am

Re: avoiding an infinite loop with JavaScript String.replace

Post by amir_e_a »

BenoitRen wrote:Try replacing "Friedman " (with a space), "Friedman." and "Friedman," instead. That should take care of most occurences, depending if colons and semi-colons are also used.


Thanks, but it's a very specific case. Isn't there a generic way to do a global replacement, but only once? That's what most text editors do, and also Perl's s///g substitution operator.

I tested it in Firefox, Chrome and IE and it happens everywhere. Is it a part of the specification of the String.replace method? Or am i missing something? (I am really not a JS expert.)
User avatar
trolly
Moderator
Posts: 39851
Joined: August 22nd, 2005, 7:25 am

Re: avoiding an infinite loop with JavaScript String.replace

Post by trolly »

Two step replacing? e.g. Friedman -> Friedmax_ -> Friedmann
I always do that if i have that problem.
Think for yourself. Otherwise you have to believe what other people tell you.
A society based on individualism is an oxymoron. || Freedom is at first the freedom to starve.
Constitution says: One man, one vote. Supreme court says: One dollar, one vote.
User avatar
amir_e_a
Posts: 22
Joined: December 9th, 2005, 5:03 am

Re: avoiding an infinite loop with JavaScript String.replace

Post by amir_e_a »

trolly wrote:Two step replacing? e.g. Friedman -> Friedmax_ -> Friedmann
I always do that if i have that problem.


This is a possible workaround, but it's quite ugly.

In vi i can do %s/Friedman/Friedmann/g

In Perl i can do $string =~ s/Friedman/Friedmann/g;

In Windows' Notepad i can do Search and Replace, then Replace all and it only runs once to the end of the file.

Isn't it possible in JavaScript?
User avatar
trolly
Moderator
Posts: 39851
Joined: August 22nd, 2005, 7:25 am

Re: avoiding an infinite loop with JavaScript String.replace

Post by trolly »

This looks promising: http://www.tizag.com/javascriptT/javasc ... eplace.php

Second part: Regular expressions
Think for yourself. Otherwise you have to believe what other people tell you.
A society based on individualism is an oxymoron. || Freedom is at first the freedom to starve.
Constitution says: One man, one vote. Supreme court says: One dollar, one vote.
User avatar
dickvl
Posts: 54145
Joined: July 18th, 2005, 3:25 am

Re: avoiding an infinite loop with JavaScript String.replace

Post by dickvl »

Works fine with regular expressions:

Code: Select all

javascript: var article = "Alex Friedman was a great pianist. Friedman was born in New York."; article = article.replace(/Friedman/g, "Friedmann");alert(article);void 0;
Post Reply