简体   繁体   English

指定javascript字符串拆分大小

[英]specifying javascript string split size

I want to separete this text with split function like this. 我想用这样的分割功能分隔此文本。 Array items should be 3 charecters. 数组项目应为3个字符。

var text = "abcdef";
var splitted = ["abc", "def"]

var text = "abcdefg";
var splitted = ["a", "bcd", "efg"]

var text = "abcdefgk";
var splitted = ["ab", "cde", "fgk"]

text split from last to first. 文本从最后到第一个分开。

var size = 3; //specify the size here
var str = "abcdefgk" //the string you want to split

var i = str.length % 3; // modulo gets you the remaining part
var result = [str.substring(0,i)]; //create array with only first part

//for the rest, iterate over str and add chunks of length size
for(; i < str.length; i+=size){
    result.push(str.substring(i, i+size))

}
alert(result) //display the result

Regex solution: 正则表达式解决方案:

var text = "abcdefg";
var regex = text.length % 3 ? '^.{'+ (text.length % 3) +'}|.{3}' : '.{3}';
var result = text.match(new RegExp(regex, 'g'));
// ["a", "bcd", "efg"]

You could also do with: 您还可以这样做:

var text = "abcdefg";
var result = [text.substr(0, text.length % 3)].concat(text.substr(text.length % 3).match(/.{3}/g));

Hint : consider .match instead of .split to get the result when using regex. 提示使用正则表达式时,考虑使用.match而不是.split来获得结果。

If you want to use recursion then try this 如果要使用递归,请尝试此

<script type="text/javascript">
    function strSplit(str){
        var arr=[];
        dorecurSplit(str,str.length,arr,0);
        alert(arr);
    }
    function dorecurSplit(st,len,ar,x)
    {
        if(len<=0)
            return ar;
        else{
            var i=len%3;
            if(i==0)
                {
                    ar.push(st.substring(x,x+3));
                    dorecurSplit(st,len-3,ar,x+3);
                }
            else
                {
                    ar.push(st.substring(x,i));
                    dorecurSplit(st,len-i,ar,x+i);
                }
        }

    }
</script>

to call this 叫这个

<button onclick="strSplit('abcdefghij')">Split it</button>

For the desired output, we need to use substring, since the smaller section is taken at beginning than at end. 对于所需的输出,我们需要使用子字符串,因为在开始时采用的是比结尾小的段。 Note: we do not use slice, because negative index in slice will take characters from string from end again. 注意:我们不使用slice,因为slice中的负索引将再次从字符串末尾获取字符。 So, we stick with substring to avoid that. 因此,我们坚持使用子字符串来避免这种情况。

var str = "abcdefgh";
var len = str.length;
var res = [];

for(var i=len;i>0;i=i-3){
 res.push(str.substring(i-3,i));
}
//res array will contain the desired output now

But, if parsing should be greedy form beginning, ie {3,3,2} instead of {2,3,3} then, use the regex ( /[a-zA-Z]{1,3}/gi ) for this,which is easier and faster.For this we do not need substring. 但是,如果解析应该以贪婪的形式开始,即以{3,3,2}而不是{2,3,3}进行解析,则使用正则表达式( / [a-zA-Z] {1,3} / gi )这,它更容易,更快。为此,我们不需要子字符串。 var str = "abcdef"; var str =“ abcdef”; var result = str.match(/[a-zA-Z]{1,3}/gi); var result = str.match(/ [a-zA-Z] {1,3} / gi); //["abc","def"] // [ “ABC”, “DEF”]

var str = "abcdefgh";
var result = str.match(/[a-zA-Z]{1,3}/gi); //["abc","def","gh"]

var str = "abcdefg";
var result = str.match(/[a-zA-Z]{1,3}/gi); //["abc","def","g"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM