I want output be like this:
['YY','RR']
with no empty elements
function pntSq(m){
let a = m.split(/([A-Z][A-Z])/gi)
console.log(a)
}
pntSq("YYRR")
// output :
// [ '', 'YY', '', 'RR', '' ]
I want output be like this:
['YY','RR']
with no empty elements
function pntSq(m){
let a = m.split(/([A-Z][A-Z])/gi)
console.log(a)
}
pntSq("YYRR")
// output :
// [ '', 'YY', '', 'RR', '' ]
You can match
instead of using split.
function pntSq(m){
return m.match(/[A-Z]{2}/g);
}
console.log(pntSq('YYRR'));
JavaScript may not support splitting on lookbehinds. One workaround would be to do this in two steps. First we can replace on the following regex pattern:
([A-Z])(?!\1)(?=.)
This will capture any capital which in turn is followed by a different letter (and also not the end of the string). Then, we replace with a single space. As the second step, we simply split on space to get the output array we want.
function pntSq(m) {
m = m.replace(/([A-Z])(?!\1)(?=.)/g, "$1 ");
console.log(m);
var a = m.split(/ /g);
console.log(a);
}
pntSq("YYRRAAABBBBBCC");