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)
To remove curly braces, we will use javascript replace() method and some regex code. Javascript replace() method also accept regex code to do the operation and we will write regex code to search curly braces global level in the string.
To remove square brackets from the beginning and end of a string using Python, we pass “[]” to the strip() function as shown below. If you have curly brackets as well, we pass “[]{}” to strip() to remove the brackets.
String n = s. replaceAll("/{", " "); String n = s. replaceAll("'{'", " ");
If one needs to use the replace() function to replace any open curly brackets "{" contained within a String with another character, you need to first escape with a backslash "\".
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, '');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With