-2

Supposing, I have a string ASDFZXCVQW, is it possible to capture this into groups of N, and then the remaining characters would be in the final group.

For example, if N were 4, then we could have: ASDF, ZXCV, and QW. Notice how the QW is everything that is left over.

I know how to capture the groups of N with .{N}, and then manually get the leftover through string indexing, but is it possible to do this in a single regular expression?

nodel
  • 471
  • 6
  • 22

2 Answers2

0

var data = 'ASDFZXCVQW'

var result = data.match(/\D{1,4}/g)

console.log(result)

It will be helpful!

Jay Parmar
  • 368
  • 2
  • 9
0

That really depends on the language in use. In general, it will be a concatenation of 0 or more 4-character groups followed by 0-3 single characters. Here is a possible formal definition for alphanumeric string: ([a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9])*[a-zA-Z0-9]*. Different languages might express this differently and possibly in a more compact way.

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85