Beware of the "replaceAll" string function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2015 03:11 PM
We recently had a situation where our event processor hung. Turns out, it was a result of the following:
var msg = '$&';
var src = '<div ${style_section_content}>${CONTENT}</div>';
src = src.replaceAll('${CONTENT}',msg);
gs.print(src);
Go ahead and try in (in a Fix Script or a Background script..it won't return...there's an infinite loop). For us, there was logic like this as part of an email notification. This caused our event processor to hang.
The problem is in a global out of the box business rule called String class extensions:
String.prototype.replaceAll = function(from, to) {
var str = this;
var idx = str.indexOf( from );
while ( idx > -1 ) {
str = str.replace( from, to );
idx = str.indexOf( from );
}
The '$&' has special meaning to replace and when combined with the looping logic, you get an infinite loop.
- Labels:
-
Integrations

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2015 01:39 AM
Hi Ty, did you create an Incident for this behaviour? What version are you running?
Btw. if you use Fuji, you can use just standard regular expressions:
var msg = '$&';
var src = '<div ${style_section_content}>${CONTENT}</div>';
src= src.replace(/\$\{CONTENT\}/g, msg);
gs.print(src);
That should work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2015 05:45 AM
Hi Julian,
Yes, we have a HI system ticket and they are opening a problem ticket (did not get a number from them yet, as soon as I do, I will post).
We are in Dublin (patch 8).
Good to know about the fuji version allowing regex in the replace.
replaceAll() has problems.
Here's an even simpler version to show a problem:
'ABCD'.replaceAll('A','AA');