如何替换数组中的某些子串?

时间:2017-07-19 09:31:43

标签: javascript arrays regex

我的数据集采用正常的数据模型格式:

var data = "Billing, alpha, beta"

但是我想通过替换","来格式化数组来修改这些数据。用" / text / i"

var newData = [/Billing/i , /alpha/i, /beta/i]

以下是我尝试的代码:

var data   = "Billing, alpha, beta"
var y = x.split(',')
var newData  = y.replace(",", "/i,")
console.log(newData)

1 个答案:

答案 0 :(得分:-1)

试试这个代码段

var data   = "Billing, alpha, beta";
var y = data.split(',');
newData=[];
for(i=0;i<y.length;i++)
  newData.push(new RegExp( y[i].replace(/(^\s+|\s+$)/g,""),"i"));
console.log(newData);