JSFiddle - React, Tailwind, and code Playground
JavaScript
var teststr = "SL\nSL 4BZ\nSL4BZ\nSL4\nSL4 4BZ\nSL44BZ\nDY10\nDY10 3BD\nDY10 3BD";
var arr = teststr.match( /^[A-Z0-9]{2,4}(?=(?:\s*[A-Z0-9]{3})?$)/mg );
var res = "Using match() -> Capture group 0\nteststr.match(/^[A-Z0-9]{2,4}(?=(?:\s*[A-Z0-9]{3})?$)/mg)\n";
for (var index = 0; index < arr.length; index++)
{
res += index + ": " + arr[index] + "\n";
}
var re = /^([A-Z0-9]{2,4})(?:\s*[A-Z0-9]{3})?$/mg;
res += "\nUsing exec() -> Capture group 1\nvar re = /^([A-Z0-9]{2,4})(?:\s*[A-Z0-9]{3})?$/mg;\nre.exec(teststr)\n";
index = 0;
while ((arr = re.exec(teststr)) != null)
{
res += index + ": " + arr[1] + "\n";
index++;
}
alert( res );