MozillaZine

avoiding an infinite loop with JavaScript String.replace

Discuss how to use and promote Web standards with the Mozilla Gecko engine.
amir_e_a

User avatar
 
Posts: 22
Joined: December 9th, 2005, 5:03 am

Post Posted February 16th, 2009, 2:06 am

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.

BenoitRen

User avatar
 
Posts: 5926
Joined: April 11th, 2004, 10:20 am
Location: Belgium

Post Posted February 16th, 2009, 4:42 am

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.

amir_e_a

User avatar
 
Posts: 22
Joined: December 9th, 2005, 5:03 am

Post Posted February 16th, 2009, 4:58 am

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.)

trolly
Moderator

User avatar
 
Posts: 39899
Joined: August 22nd, 2005, 7:25 am

Post Posted February 16th, 2009, 6:35 am

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.

amir_e_a

User avatar
 
Posts: 22
Joined: December 9th, 2005, 5:03 am

Post Posted February 16th, 2009, 7:03 am

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?

trolly
Moderator

User avatar
 
Posts: 39899
Joined: August 22nd, 2005, 7:25 am

Post Posted February 16th, 2009, 7:20 am

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.

dickvl

User avatar
 
Posts: 52444
Joined: July 18th, 2005, 3:25 am

Post Posted February 16th, 2009, 6:29 pm

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;

Return to Web Development / Standards Evangelism


Who is online

Users browsing this forum: No registered users and 1 guest

cron