最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin5PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far