javascript - Replacing all square brackets with curly brackets - Stack Overflow

I have a string "[[[[[]]]]]" and I want to convert it in "{{{{{}}}}}". I tried &quo

I have a string "[[[[[]]]]]" and I want to convert it in "{{{{{}}}}}". I tried "[[[[[]]]]]".replace(/[/g,'{').replace(/]/g,'}') but not luck so far.

I have a string "[[[[[]]]]]" and I want to convert it in "{{{{{}}}}}". I tried "[[[[[]]]]]".replace(/[/g,'{').replace(/]/g,'}') but not luck so far.

Share Improve this question asked Feb 14, 2017 at 14:12 ozilozil 7,1159 gold badges36 silver badges61 bronze badges 2
  • 8 The [ is a special char in a regex, it must be escaped. – Wiktor Stribiżew Commented Feb 14, 2017 at 14:13
  • :P and you were so close – Fallenhero Commented Feb 14, 2017 at 14:25
Add a ment  | 

2 Answers 2

Reset to default 14

The [ square bracket has to be escaped.

var string = "[[[[[]]]]]";

console.log(string.replace(/\[/g, '{').replace(/]/g, '}'));

Try this:

var string = '[[[[]]]]';
console.log(string.replace(/\[/g, "{").replace(/\]/g, "}"));

https://jsfiddle/fcte0szn/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743598737a4476789.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信