Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to remove curly bracers from a string [duplicate]

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)

like image 333
nocturn4l toyou Avatar asked May 29 '12 02:05

nocturn4l toyou


People also ask

How do you remove curly braces from string?

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.

How do you remove curly braces from string in Python?

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.

How do you remove curly braces from JSON string in Java?

String n = s. replaceAll("/{", " "); String n = s. replaceAll("'{'", " ");

How do you change curly brackets in Java?

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 "\".


1 Answers

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, '');
like image 55
ziad-saab Avatar answered Sep 22 '22 09:09

ziad-saab