4

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?

Yoshi
  • 54,081
  • 14
  • 89
  • 103
GrayHat
  • 129
  • 1
  • 6
  • 1
    Does this answer your question? [Regex to match when a string is present twice](https://stackoverflow.com/questions/8186384/regex-to-match-when-a-string-is-present-twice) – FishSaidNo Oct 27 '21 at 04:42
  • I've seen that answer. There the mapping goes to a certain expression. And in my case, I need to check the characters. – GrayHat Oct 27 '21 at 04:58
  • @Fish, also, the question you cited is searching for instances of a given word. Here we are looking for any character that appears exactly twice. – Cary Swoveland Oct 27 '21 at 05:25
  • Ahh, good point, *any* character being the key here – FishSaidNo Oct 27 '21 at 05:36
  • @Fish, I deleted my answer until and if I can fix it to address your concern. It appears that regex engine satisfies the two lookaheads independently, rather than trying to find a solution that satisfies both. – Cary Swoveland Oct 27 '21 at 06:12
  • So the input is like a string made of words separated by comma + space? like "afaa, baar, food, pk, hor, pee". Right? – Carlo Moretti Oct 27 '21 at 07:11
  • @Onheiron, I think you can assume those are test strings (`'asdf'`, `'fdas'`, and so on). Even if they are comma-separated in a string the crux of the problem is to determine whether a string contains a character that appears exactly twice. – Cary Swoveland Oct 27 '21 at 07:20
  • This might be a starting point?!: https://stackoverflow.com/questions/8055727/negating-a-backreference-in-regular-expressions – Yoshi Oct 27 '21 at 07:56

2 Answers2

5

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))
}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
2

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;
}
TBA
  • 1,921
  • 4
  • 13
  • 26