开发者

Javascript - easy way to split/devide string by pattern into N parts where last part is rest of the string

If i have the strings:

开发者_高级运维
string1 = "1 secondpart this is the third part"
string2 = "14   secondpartagain   third part   again"

and i want to split them by a pattern (like whitespace) into N parts (like 3 parts), then the result should be something like:

part1: "1", part2: "secondpart", part3: "this is the third part"
  -and
part1: "14", part2: "secondpartagain", part3: "third part   again"

In Ruby i would do something like:

part1, part2, part3= string1.split(/\s+/, 3)

If i use the split function in Javascript i get something like this:

var myArray1 = string1.split(/\s+/, 3);
var myArray2 = string2.split(/\s+/, 3);

myArray1[0]: "1", myArray1[1]: "secondpart", myArray1[2]: "this"
  -and
myArray2[0]: "14", myArray2[1]: "secondpartagain", myArray2[2]: "third"

What is the easiest way to split a string by a pattern into N parts where last part is rest of the string?


If you want to split on the first two (groups of) whitespace:

function splitString(text) {
    var rexSplitter = /^(\S*)\s+(\S*)\s+(.*)$/,
        match = rexSplitter.exec(text);
    if (match) {
        return Array.prototype.slice.call(match, 1, 3);
    } 
}

or, if you’re into short code:

function splitString(text) {
     var match = /^(\S*)\s+(\S*)\s+(.*)$/.exec(text);
     return match && Array.prototype.slice.call(match, 1, 3);
}


function partitioner(original, pattern, num_parts, c) {
    var parts = original.split(pattern);
    var result = [];

    for (var i = 0; i < num_parts - 1 && parts.length > 0; i++) {
        var part = parts.shift();
        result.push(part);
    }

    result.push(parts.join(c));

    return result;
}

var string1 = "1 secondpart this is the third part";
var splitted = partitioner(string1, /\s+/, 3, ' ');
console.log(splitted);


If you like one liners you could do this:

var str = "1 secondpart this is the third part";
var arr = str.split(/\s+/,2).concat(str.replace(/^\s*\S+\s+\S+\s+/,''));


I am the author of the original question. In my specific case is num_parts=3 and divide_pattern=whitespace. In that case, Martijns answer is okay.

Björns attempt to make a general solution is also okay - but in the last part of the string, the multible whitespaces are unfortunately replaced by single spaces.

I have made a combination of the 2 former answers, which should work as some sort of generalt Split-function. This one accepts Words but can easily be modified to accept other patterns:

function divideString(orig_string, divide_pat, num_parts) {
    var regExPattern = /^\s*/.source;
    for (var i = 0; i < num_parts - 1; i++) {
        regExPattern += /(\w+)/.source + divide_pat.source;
    }
    regExPattern += /(.*)$/.source;
    var re = new RegExp(regExPattern); 

    var match = re.exec(orig_string);
    if (match) {
        return match.slice(1, num_parts+1);
    }
}

var string2 = "14   secondpartagain   third part   again";

alert( divideString(string2, /\s+/, 3) );


See java docs on string splitting: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)

It should do what you need, and I am 100% sure this works in java script.

var s="1 secondpart this is the third part";
        var d=s.split(" ");
        var l=d.length;
        document.writeln("<p> Length:"+l+"</p>");
        for(var i=0; i<l; i++){
            document.writeln("<p> "+i+":"+d[i]+"</p>");
        }

        document.writeln("<p> End </p>");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜