admin管理员组文章数量:1023230
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 badges5 Answers
Reset to default 3Modern 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.
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 badges5 Answers
Reset to default 3Modern 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.
本文标签: javascriptHow to split string with subtotal prefixStack Overflow
版权声明:本文标题:javascript - How to split string with subtotal prefix - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745598476a2158314.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论