0

I have a string.

var string = "31415926535897932384626433832795028841971693993751058209749445923078164";

I want to split it into an array of length 10 from back.

I have:

function split(a) {
  return a.split( /(?=(?:..........)*$)/ ).map(function(x){return parseInt(x)});
}

which gives me the desired output as:

[3, 1415926535, 8979323846, 2643383279, 5028841971, 6939937510, 5820974944, 5923078164]

Questions:

  • How do I make the above function dynamic so I can break strings to n number of characters? (Currently I am adding/removing dots)

  • How do I skip the first character when splitting? (I would like first element to be always 3 so second element can be of length 1 to n)?

4
  • 1
    Instead of ?:.........., do ?:.{10}. Commented Jul 20, 2015 at 5:02
  • 1
    Why would you overcomplicate some simple operations on string split by index into some compilcated regex? Commented Jul 20, 2015 at 5:06
  • @texasbruce what would be the simple way of doing it? Commented Jul 20, 2015 at 5:31
  • @Derek朕會功夫 I tried having a variable like {n} but it didnot work. I found out we cant really pass variables in regex without using RegExp constructor. Commented Jul 20, 2015 at 5:38

3 Answers 3

6

Just specify the number of digits you want to get for each item inside curly braces. And note that you can't pass variable to the regex which uses / as delimiters. You have to use RegExp constructor to pass variables in regex.

var string = "31415926535897932384626433832795028841971693993751058209749445923078164";
function split(a,n) {
  return a.split( new RegExp("(?=(?:.{" + n + "})*$)" )).map(function(x){return parseInt(x)});
}
alert(split(string, 10))

OR

You may simply use match instead of split.

string.match(/(?!^.).{11}/gm)

DEMO

var string = "31415926535897932384626433832795028841971693993751058209749445923078164";
function split(a,n) {
  return a.match(new RegExp("(?!^.).{" + n + "}|^.", "gm")).map(function(x){return parseInt(x)});
}
alert(split(string, 11))

If you also want to match the remaining chars, ie, the char present at the start and the unmatched chars exists at the last, you may use this regex.

/(?!^.).{11}|^.|.+/gm

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

This works perfectly for me. Great solution. In my jsfiddle I've wrapped it up in a helper function. jsfiddle.net/kieranpotts/8wjszvc5/1
Perfect. Thank you Avinash. Much appreciated!
1

Please check this

var _mySplit = function(str, splitLength) {
    var _regEx = '';
    var startSubStringLength = str.length % splitLength
    if(startSubStringLength > 0) {
        _regEx = new RegExp("(^.{1," + startSubStringLength + "})|(.{1," + splitLength +  "})|(.{1,})", "g")
    }
    return str.match(_regEx)
}

1 Comment

Thanks Harpeet. I will do some performance testing later with and without regex when jsperf is back up.
1

the easy way..without using RegExp var string="31415926535897932384626433832795028841971693993751058209749445923078164"; var arr = new Array(); while (!string.length < 10) { substr = string.substr(strlen(string)-10); string = string.replace(substr, ''); arr.push(substr); } var result_arr = arr.reverse();

2 Comments

This is PHP rather than JavaScript. But if you could convert this to JS @Aniket, this would be the better answer probably, since it does not rely on regexes. What do you think? :)
Thank you Aniket. jsperf is down at this time. I will do some performance testing with all the solutions when its back up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.