0
  • I have one string

     var tStr="[[[{"Name":"A","No":"8000444284"}],{"Name":"B","No":"8485990983"}],{"Name":"C","No":"0000000000"}]";
    
  • In this string i try to remove all [ and ]

My Code is:

function trim(str, characters) 
{
  var c_array = characters.split('');
  var result  = '';
  for (var i=0; i < characters.length; i++)
    result += '\\' + c_array[i];
  return str.replace(new RegExp('^[' + result + ']+|['+ result +']+$', 'g'), '');
}

And this Function Use as

trim(str,'[ ]');

In Calling var tStr="[[[{"Name":"A","No":"8000444284"}],{"Name":"B","No":"8485990983"}],{"Name":"C","No":"0000000000"}]";

thats not working please any one help me...:)

2
  • What is the problem with replace? Commented Jan 2, 2015 at 6:36
  • you can use .replace() method str.replace('[',''); Commented Jan 2, 2015 at 6:37

1 Answer 1

3
\[|\]

Try this.Replace by empty string.See demo.

https://www.regex101.com/r/fG5pZ8/4

var re = /\[|\]/g;
var str = '[[[{"Name":"A","No":"8000444284"}],{"Name":"B","No":"8485990983"}],{"Name":"C","No":"0000000000"}]';
var subst = '';

var result = str.replace(re, subst);
Sign up to request clarification or add additional context in comments.

4 Comments

Error Comes : Uncaught TypeError: undefined is not a function at the replace() function line
the actual purpose is i have dictionary and that i convert into string than after convert into the dictionary then push one array and than using JSON storing into local-storage.
@virbhadrasinhGohil should work.Check d demo.the code is working .There is problem somewhere else in ur code
ya ya you are right, your answer is perfect ,this string is into the dictionary without convert into string i'll do this,, thx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.