Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

added 1 character in body
Source Link
Kijewski
  • 26.1k
  • 14
  • 108
  • 149

Use:

str = str.replace(/[""]/g, '"');
str = str.replace(/[‘’]/g,"'");

or to do it in one statement:

str = str.replace(/[""]/g, '"').replace(/[‘’]/g,"'");

In JavaScript (as in many other languages) strings are immutable - string "replacement" methods actually just return the new string instead of modifying the string in place.

The MDN JavaScript reference entry for replace states:

Returns a new string with some or all matches of a pattern replaced by a replacement.

...

This method does not change the String object it is called on. It simply returns a new string.

Use:

str = str.replace(/[""]/g, '"');
str = str.replace(/[‘’]/g,"'");

or to do it in one statement:

str = str.replace(/[""]/g, '"').replace(/[‘’]/g,"'");

In JavaScript (as in many other languages) strings are immutable - string "replacement" methods actually just return the new string instead of modifying the string in place.

The MDN JavaScript reference entry for replace states:

Returns a new string with some or all matches of a pattern replaced by a replacement.

...

This method does not change the String object it is called on. It simply returns a new string.

Use:

str = str.replace(/[""]/g, '"');
str = str.replace(/[‘’]/g,"'");

or to do it in one statement:

str = str.replace(/[""]/g, '"').replace(/[‘’]/g,"'");

In JavaScript (as in many other languages) strings are immutable - string "replacement" methods actually just return the new string instead of modifying the string in place.

The MDN JavaScript reference entry for replace states:

Returns a new string with some or all matches of a pattern replaced by a replacement.

...

This method does not change the String object it is called on. It simply returns a new string.

Being the canonical answer, it should express doubtlessness :-)
Source Link
Bergi
  • 670.9k
  • 162
  • 1k
  • 1.5k

TryUse:

str = str.replace(/[""]/g, '"');
str = str.replace(/[‘’]/g,"'");

or to do it in one statement:

str = str.replace(/[""]/g, '"').replace(/[‘’]/g,"'");

I'm not aIn JavaScript expert, but(as in many other languages) strings are immutable - string "replacement" methods actually just return the new string instead of modifying the string in place. (Bizarrely enough, the few JavaScript string tutorials I've just looked up don't state a string's immutability - something I'd expect to be in the first or second paragraph of a C# or Java string tutorial.)

Confirmation comes fromThe the Mozilla Core JavaScript reference entryMDN JavaScript reference entry for replacereplace states:

Returns a new string with some or all matches of a pattern replaced by a replacement.

... ...

This method does not change the String object it is called on. It simply returns a new string.

Try:

str = str.replace(/[""]/g, '"');
str = str.replace(/[‘’]/g,"'");

or to do it in one statement:

str = str.replace(/[""]/g, '"').replace(/[‘’]/g,"'");

I'm not a JavaScript expert, but in many languages strings are immutable - string "replacement" methods actually just return the new string instead of modifying the string in place. (Bizarrely enough, the few JavaScript string tutorials I've just looked up don't state a string's immutability - something I'd expect to be in the first or second paragraph of a C# or Java string tutorial.)

Confirmation comes from the Mozilla Core JavaScript reference entry for replace:

Returns a new string with some or all matches of a pattern replaced by a replacement.

...

This method does not change the String object it is called on. It simply returns a new string.

Use:

str = str.replace(/[""]/g, '"');
str = str.replace(/[‘’]/g,"'");

or to do it in one statement:

str = str.replace(/[""]/g, '"').replace(/[‘’]/g,"'");

In JavaScript (as in many other languages) strings are immutable - string "replacement" methods actually just return the new string instead of modifying the string in place.

The MDN JavaScript reference entry for replace states:

Returns a new string with some or all matches of a pattern replaced by a replacement.

...

This method does not change the String object it is called on. It simply returns a new string.

added 388 characters in body
Source Link
Jon Skeet
  • 1.5m
  • 894
  • 9.3k
  • 9.3k

Try:

str = str.replace(/[""]/g, '"');
str = str.replace(/[‘’]/g,"'");

or to do it in one statement:

str = str.replace(/[""]/g, '"').replace(/[‘’]/g,"'");

I'm not a JavaScript expert, but in many languages strings are immutable - string "replacement" methods actually just return the new string instead of modifying the string in place. (Bizarrely enough, the few JavaScript string tutorials I've just looked up don't state a string's immutability - something I'd expect to be in the first or second paragraph of a C# or Java string tutorial.)

Confirmation comes from the Mozilla Core JavaScript reference entry for replace:

Returns a new string with some or all matches of a pattern replaced by a replacement.

...

This method does not change the String object it is called on. It simply returns a new string.

Try:

str = str.replace(/[""]/g, '"');
str = str.replace(/[‘’]/g,"'");

or to do it in one statement:

str = str.replace(/[""]/g, '"').replace(/[‘’]/g,"'");

I'm not a JavaScript expert, but in many languages strings are immutable - string "replacement" methods actually just return the new string instead of modifying the string in place. (Bizarrely enough, the few JavaScript string tutorials I've just looked up don't state a string's immutability - something I'd expect to be in the first or second paragraph of a C# or Java string tutorial.)

Try:

str = str.replace(/[""]/g, '"');
str = str.replace(/[‘’]/g,"'");

or to do it in one statement:

str = str.replace(/[""]/g, '"').replace(/[‘’]/g,"'");

I'm not a JavaScript expert, but in many languages strings are immutable - string "replacement" methods actually just return the new string instead of modifying the string in place. (Bizarrely enough, the few JavaScript string tutorials I've just looked up don't state a string's immutability - something I'd expect to be in the first or second paragraph of a C# or Java string tutorial.)

Confirmation comes from the Mozilla Core JavaScript reference entry for replace:

Returns a new string with some or all matches of a pattern replaced by a replacement.

...

This method does not change the String object it is called on. It simply returns a new string.

Source Link
Jon Skeet
  • 1.5m
  • 894
  • 9.3k
  • 9.3k
Loading
lang-js

AltStyle によって変換されたページ (->オリジナル) /