使用javascript删除字符串中的特定字符

时间:2015-01-02 06:35:29

标签: javascript regex

  • 我有一个字符串

     var tStr="[[[{"Name":"A","No":"8000444284"}],{"Name":"B","No":"8485990983"}],{"Name":"C","No":"0000000000"}]";
    
  • 在此字符串中,我尝试删除所有[]

我的代码是:

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'), '');
}

此功能用作

trim(str,'[ ]');

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

那不起作用请任何人帮助我...... :)

1 个答案:

答案 0 :(得分:3)

\[|\]

试试这个。empty string。见。演示。

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);