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

149
Views
regex character appearing exactly two times

Given an array of strings, test if there's any character appeared exactly two times.

Sample:

const input = ['asdf', 'fdas', 'asds', 'd fm', 'dfaa', 'aaaa', 'aabb', 'aaabb'];
const output = ['asds', 'dfaa', 'aabb', 'aaabb'];

I tried like this

/(?:^|(.)(?!\1))([A-Za-z0-9])\2(?!\2)/

but I got only these strings: ['dfaa', 'aabb', 'aaabb'].

Please any suggestions?

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

0

You can use

/(.)(?<=^(?:(?!\1).)*\1(?=(?:(?!\1).)*\1(?:(?!\1).)*$))/s

See the regex demo. Note that since we are lookiing for the first match only, there is no g flag. Details:

  • (.) - Any one char captured into Group 1:
  • (?<= - start of a positive lookbehind, immediately on the left, there should be
    • ^ - start of string
    • (?:(?!\1).)* - any char, as many as possible, other than a char captured into Group 1
    • \1 - the same char captured into Group 1
    • (?=(?:(?!\1).)*\1(?:(?!\1).)*$) - a positive lookahead that requires any zero or more chars other than the char in Group 1, then the same char as captured in Group 1, and again any zero or more chars other than the char in Group 1 till end of string
  • ) - end of the lookbehind.

The lookarounds with tempered greedy tokens inside ensure the captured char only appears exactly twice in a string.

See the JavaScript demo:

const input = ['asdf', 'fdas', 'asds', 'd fm', 'dfaa', 'aaaa', 'aabb', 'aaabb'];
const re = /(.)(?<=^(?:(?!\1).)*\1(?=(?:(?!\1).)*\1(?:(?!\1).)*$))/s;
for (const s of input) {
   console.log(s, '=>', re.test(s))
}

about 4 years ago · Juan Pablo Isaza Report

0

For Example, /[^a]/gm: Match a single character not present in the list below.

const sampleArray = [ 'asdf', 'fdas', 'asds', 'd fm', 'dfaa', 'aaaa', 'aabb', 'aaabb'];

var sampleOutput = [];

sampleArray.forEach(function (item, index) {
  var dupResult = checkDuplicate(item);
  if(dupResult){
    sampleOutput.push(item);
  }
});

console.log(sampleOutput);

function checkDuplicate(item){
    var isCountTwice = false;
    for(var i = 0; i < item.length; i++){
        if(item.replace(new RegExp("[^"+ item[i] +"]","gm"), "").length == 2){
            isCountTwice = true;
        }
    } 
    return isCountTwice;
}

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!