Home > Net >  regex character appearing exactly two times
regex character appearing exactly two times

Time:10-27

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?

CodePudding user response:

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))
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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;
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related