javascript - How to split string with subtotal prefix - Stack Overflow

I want to split string'something.somethingMore.evenMore[123].last'to['something',&

I want to split string

'something.somethingMore.evenMore[123].last'

to

['something',
'something.somethingMore',
'something.somethingMore.evenMore[123]',
'something.somethingMore.evenMore[123].last']

I can't figure out simple solution to split string by separator '.', but with preceding content of string.

I want to split string

'something.somethingMore.evenMore[123].last'

to

['something',
'something.somethingMore',
'something.somethingMore.evenMore[123]',
'something.somethingMore.evenMore[123].last']

I can't figure out simple solution to split string by separator '.', but with preceding content of string.

Share Improve this question edited Dec 17, 2017 at 15:06 ekad 14.6k26 gold badges46 silver badges48 bronze badges asked Sep 11, 2014 at 12:57 Peter DubPeter Dub 5311 gold badge6 silver badges14 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

Modern JS programming style would be something like

str                                             //take the input
    .split('.')                                 //and split it into array of segments,
    .map(                                       //which we map into a new array,
        function(segment, index, segments)      //where for each segment
            return segments.slice(0, index+1)   //we take the segments up to its index
                .join(".")                      //and put them back together with '.'
            ;
         }
    )

This takes advantage of the fact that the Array#map function passes not only the current value, but also its index and the entire input array. That allows us to easily use slice to get an array containing all the segments up to the current one.

I wouldn't call my solution simple but:

function splitArrayIntoGoofyArray(val){
  var str =val.split("."), arr = [];
  for (var x = str.length; x >= 1; x--){
     var cnt = str.length - x;
     arr.push((cnt > 0) ? arr[cnt - 1] + "." + str[cnt] : str[cnt]);
  }
  return arr;
}
console.log(splitArrayIntoGoofyArray("aaa.bbbc.ddd"));

http://jsfiddle/7tav4sue/

It depends on what is simple solution. The easiest way to do it is to create first array with

var str = 'something.somethingMore.evenMore[123].last';
var arr = str.split('.');

And then fill new array with this data

var result = [];
for (var i = 0; i < arr.length; i++) {
    var item = [];
    for (var j = 0; j <= i; j++) {
        item.push(arr[j]);
    }
    result.push(item.join('.'))
}

You could try matching instead of splitting,

> var s = 'something.somethingMore.evenMore[123].last';
undefined
> var regex = /(?=(^((([^.]*)\.[^.]*)\.[^.]*)\.[^.]*))/g;
undefined
> regex.exec(s)
[ '',
  'something.somethingMore.evenMore[123].last',
  'something.somethingMore.evenMore[123]',
  'something.somethingMore',
  'something',
  index: 0,
  input: 'something.somethingMore.evenMore[123].last' ]

To get the desired output,

> regex.exec(s).slice(1,5)
[ 'something.somethingMore.evenMore[123].last',
  'something.somethingMore.evenMore[123]',
  'something.somethingMore',
  'something' ]

I guess you'll need a loop :

while finding "." character, return in a list substring till new "." index. Use the last correct index found to start each search.

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

相关推荐

  • javascript - How to split string with subtotal prefix - Stack Overflow

    I want to split string'something.somethingMore.evenMore[123].last'to['something',&

    10小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信