Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
28
rated 0 times [  35] [ 7]  / answers: 1 / hits: 15279  / 12 Years ago, tue, may 29, 2012, 12:00:00

I did a search through the site, but it's still not working.



var blah2 = JSON.stringify({foo: 123, bar: <x><y></y></x>, baz: 123});


this is what I tried:



blah2.replace(/[{}]/g, );


this is what it the string comes out to:



got {baz:123,foo:123}


(i know this is probably a newb question, but this is my first time working with javascript and i just don't know what i'm missing)


More From » json

 Answers
38

Javascript strings are immutable. When you call blah2.replace, you are not replacing something inside blah2, you are creating a new string. What you probably want is:



blah2 = blah2.replace(/[{}]/g, '');

[#85297] Sunday, May 27, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elliem

Total Points: 415
Total Questions: 117
Total Answers: 94

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
;