Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

143
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda