我在这里有这个字符串
"\n\nFlour (Contains: Wheat Flour,\n\nMalted Barley Flour, Niacin,\n\nReduced Iron, Thiamine Mononi-\n\ntrate, Riboflavin, Folic Acid), Water,\n\nSourdough (6.4%) (Contains: Water,\n\nFlour [Wheat Flour, Malted Barley\nFlout, Niacin, Reduced Iron,\nThiamine Mononitrate, Riboflavin,\nFolic Acid], Yeast), Salt, Wheat\nGerm Semolina (Contains: Durum\nWheat Semolina, Niacin, Ferrous\nSulphate, Thiamine Mononitrate,\nRiboflavin, Folic Acid).\n"
运行简单的data.split(',')
和this.ingredients[i] = this.ingredients[i].replace(/(\r\n|\n|\r)/gm,"").trim()
请给我以下内容
"Flour (Contains: Wheat Flour",
"Malted Barley Flour",
"Niacin",
"Reduced Iron",
"Thiamine Mononi-trate",
"Riboflavin",
"Folic Acid)",
"Water",
"Sourdough (6.4%) (Contains: Water",
...
但是我想在(Contains:
处进行另外的拆分,以便使此字符串
"Flour (Contains: Wheat Flour",
将成为"Flour", "Wheat Flour",
我想知道如何使用两种不同的分隔符来实现我想做的事情。我应该使用其他方法而不是拆分吗?
答案 0 :(得分:4)
您可以在split
中使用正则表达式作为分隔符。然后使用|
指定多个定界符。
data.split(/\(Contains: |,/)