1

Could any one suggest code to remove mentioned special characters: []{}<>

Code inside the JavaScript tags:

var name="formattting[]{}<>";

rename = name.replace(/[<{>}([)]/g,"");

Output:

formattting]

Here out of six special symbols, five symbols are removed and now my concern is to remove the special character ].

timolawl
  • 5,434
  • 13
  • 29
  • You need to escape all special Characters with \ http://stackoverflow.com/questions/3115150/how-to-escape-regular-expression-special-characters-using-javascript – Citrullin Apr 15 '16 at 11:05
  • 1
    @PhilippBlum: In character classes, all you need to escape are `]`,``\`` and `-`. – Bergi Apr 15 '16 at 11:07
  • Was writing `9` instead of `]` intentional? – Bergi Apr 15 '16 at 11:11

3 Answers3

2

Try this:

rename = name.replace(/[[\]{}<>]/g, "");
timolawl
  • 5,434
  • 13
  • 29
  • old code: var name="formattting[]{}<>hdsdshd"; var removed=name.replace(/[<{>}([)]/g,""); new code thats works rename = name.replace(/[[\]{}<>]/g, ""); this new code works :) thanks for time timolawl – suresh sadanala Apr 15 '16 at 11:18
  • You're welcome! Accept this answer if it works for you. – timolawl Apr 15 '16 at 11:21
1

You need to escape special characters in regex:

var name="formattting[]{}<>";
rename = name.replace(/[\$<{>}9\(\[\)\]]/g,"");
alert(rename)
baao
  • 71,625
  • 17
  • 143
  • 203
0

Simply escape the \]

var name="formattting[]{}<>";
var rename = name.replace(/[<{>}([)\]]/g,"");

alert(rename);
mkHun
  • 5,891
  • 8
  • 38
  • 85