Doublequote issue in javascript replace method

RESOLVED INVALID

Status

()

Core
JavaScript Engine
RESOLVED INVALID
6 years ago
6 years ago

People

(Reporter: frattaro, Unassigned)

Tracking

12 Branch
x86_64
Windows 7
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

6 years ago
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0
Build ID: 20120420145725

Steps to reproduce:

POSTed to get some JSON in return using jquery. eval()'d the json, it parsed correctly. The raw JSON looked like this:

.....   "LineItemName" : "Assoc Sx's / \"Red Flags\"",  .....

Then I tried a number of combinations to try to replace the double quotes with escaped characters. Like:

json[0].LineItemName.replace('"', '"');
json[0].LineItemName.replace('\"', '"');
json[0].LineItemName.replace('\\"', '"');
json[0].LineItemName.replace('"', '\"');
json[0].LineItemName.replace('"', '\\"');
json[0].LineItemName.replace('"', '"');
json[0].LineItemName.replace('\"', '"');
json[0].LineItemName.replace('"', '"');
json[0].LineItemName.replace('\"', '"');




Actual results:

It couldn't find the second double quote, after the Red Flags text.

It finally worked when I used a regular expression to find the double quote rather than just a string value (that is, for the first argument of the .replace method).


Expected results:

It should have found and replaced the double quote with one of the string arguments listed above.
(Reporter)

Comment 1

6 years ago
I should have mentioned that I was placing this JSON data into an HTML tag's attribute, which was giving me funky results.
The replace() method by default replaces only the first match found, unless you pass the 'g' flag, which you weren't doing....
(Reporter)

Comment 3

6 years ago
(In reply to Boris Zbarsky (:bz) from comment #2)
> The replace() method by default replaces only the first match found, unless
> you pass the 'g' flag, which you weren't doing....

Why would anyone expect that behavior? Oh well. The W3C page on the replace method is vague about it but does mention the /g flag.

Thanks much

-Anton
Status: UNCONFIRMED → RESOLVED
Last Resolved: 6 years ago
Resolution: --- → INVALID
> > The replace() method by default replaces only the first match found, unless
> > you pass the 'g' flag, which you weren't doing....
> 
> Why would anyone expect that behavior?

Because it's totally commonplace for regexps implemented in countless programs.
(Reporter)

Comment 5

6 years ago
(In reply to Nicholas Nethercote [:njn] from comment #4)
> > > The replace() method by default replaces only the first match found, unless
> > > you pass the 'g' flag, which you weren't doing....
> > 
> > Why would anyone expect that behavior?
> 
> Because it's totally commonplace for regexps implemented in countless
> programs.

There's no reason to believe that this is method is restricted to regex input. It accepts two strings, and the search string is optionally a regex string.

PHP's str_replace() replaces all without a flag, c#'s replace() replaces all as well.
You need to log in before you can comment on or make changes to this bug.