0

Is there any ways to split an array of string in JavaScript based on multiple values.

var inputArray = ["[dim].[att].&[1]","[dim].[att].&[2]","[dim2].[att2].&[2]","[dim].[att].[All].Children","[dim4].[att5].[All].Children", "[m1].[123]", "[m1].[m4]"];

var splitBasedOn = `[".&",".[All].Unknown",".[All]", "."];`

so I want to split values in inputArray, based on values in splitBasedOn array.

I will first split on first value in splitBasedOn array then I will try to split based on second value in splitBasedOn and so on.

Without using multiple for loops how can I acheive desired result.

georg
  • 211,518
  • 52
  • 313
  • 390
SharpCoder
  • 18,279
  • 43
  • 153
  • 249
  • What ist he minimum complexity you need, O(n) or O(n^2)? – adricadar Mar 25 '15 at 14:51
  • possible duplicate of [How do I split a string with multiple separators in javascript?](http://stackoverflow.com/questions/650022/how-do-i-split-a-string-with-multiple-separators-in-javascript) – Radio Mar 25 '15 at 14:57
  • 1
    What's the expected result? – georg Mar 25 '15 at 15:02
  • there is confussion at one point. When we get the output of first operation then after that do you want to perform operation on that previous output (i.e Chaining of operation) ? Or you want to keep your base string same for all operations. – maddy Mar 25 '15 at 15:07

1 Answers1

0

You can make use of RegExp constructor and alternation operator | and don't forget to make use of global flag g.

inputArray.forEach(function(str){
   console.log(str.split(new RegExp(splitBasedOn.join("|"), "g"));
});
Amit Joki
  • 58,320
  • 7
  • 77
  • 95