Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
split with regexr with no empty elements

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', '' ]

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can match instead of using split.

function pntSq(m){
  return m.match(/[A-Z]{2}/g);
}

console.log(pntSq('YYRR'));

about 4 years ago · Juan Pablo Isaza Report

0

Link: regex101

This matches any 2 uppercase letters together, but note that they may be different.

[A-Z]{2}

To match the same uppercase letter 2 times. This ensures that it's the same letter repeated and not different letters.

([A-Z])\1
about 4 years ago · Juan Pablo Isaza Report

0

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");

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!