N30
N30

Reputation: 3663

IE9 javascript replace does not work

I have a string such as "Value = $val$" and I want to replace $val$ with some value like "$0.00"

so final string can look like "Value = $0.00"

This works in firefox and IE 7 and IE 8 but does not work in IE 9 any idea why? and how I can resolve the issue?

Any value other than $0.00 (e.g.$5.00) works without any issue.

http://jsfiddle.net/jhdVV/5/

edit: updated link with a textbox and a button to test with diff values.

http://jsfiddle.net/jhdVV/10/

In IE 9 I am getting "Value =$val$.00"

Browser is in Standards mode.

Note: I am working on legacy code so , ideally I would like to stay away from tempting jquery solutions.

Upvotes: 0

Views: 2418

Answers (2)

John Pick
John Pick

Reputation: 5650

The $0 in your replacement text is essentially an uninitialized variable, the behavior of which is undefined. So, escape the dollar sign:

$$0.00

Upvotes: 2

Daniel
Daniel

Reputation: 81

Any reason you're not simply doing this?

function replaceValue(source, find, replacement) {
    return source.replace(find, replacement);
}

Upvotes: 5

Related Questions