-1

I want to have a regular expression that checks if the letters A-F and a-f occur only once. How do I do this? I want to check for there only being one letter in the user's input, and that letter is either from A-F or a-f.

Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

1

" I want to check for there only being one letter in the user's input, and that letter is either from A-F or a-f"

You can use /^[a-f]$/i to match a single letter in the A-F and a-f ranges.

let rx = /^[a-f]$/i;

console.log(rx.test('a'));
console.log(rx.test('F'));
console.log(rx.test('G'));
console.log(rx.test('aa'));

If you meant that the input may be of arbitrary length, but you must ensure that there is only one letter and that letter is in the A-F and a-f ranges then you could do:

let rx = /^[^a-f]*[a-f][^a-f]*$/i;

console.log(rx.test('AF'));
console.log(rx.test('A'));
console.log(rx.test('2F3'));
console.log(rx.test('2G3'));
plalx
  • 42,889
  • 6
  • 74
  • 90
  • `A-F or a-f occur only once` does not mean the whole line only contains it, does it? – choz Jun 17 '16 at 01:22
  • He also said "there only being one letter in the user's input" – Barmar Jun 17 '16 at 01:22
  • Well, from the rest of the question it seems like there is only one letter in the input or at least that's what I understood, but I may be wrong. It isint very clear. – plalx Jun 17 '16 at 01:24
  • @plalx Ah true, he indeed mentions that. My bad.. But the question is still really unclear to me.. – choz Jun 17 '16 at 01:25
  • @choz I added both cases. – plalx Jun 17 '16 at 01:29
1

Rather than using a regular expression for this, I would just check that the input string has a length of one and that it is within the range you want:

var input = 'F';
var test = input.toLowerCase();
var aLetterCode = 97;
var fLetterCode = 102;
if (test.length() == 1 &&
    test.charCodeAt(0) >= aLetterCode &&
    test.charCodeAt(0) <= fLetterCode)
{
    console.log("input is a match.");
}             
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I like this approach. I was about to suggest [this](https://jsfiddle.net/3xg34Lmh/) actually. Just a `toLowerCase()` so you don't need to figure out both case ranges. – Dave Chen Jun 17 '16 at 01:29
  • @DaveChen Brilliant suggestion. Let me update. – Tim Biegeleisen Jun 17 '16 at 01:31
  • @DaveChen Regexes were designed for pattern matching. A simple `/^[a-f]$/i.test(...)` does the trick and is far easier to understand that these cryptic character codes. I would at least do something like `let aLetterCode = 65` and `fLetterCode = 70`. – plalx Jun 17 '16 at 01:31
  • @plalx Done. Updated per your suggestion. – Tim Biegeleisen Jun 17 '16 at 01:35
0

Not sure of the exact meaning of what you are saying... You want any of the letters used only once, or only one of the letters used once, is this in a string or only those letters?

For me in similar circumstances, regular expressions are matchers, not counters... you can match_all on [A-Fa-f] and then on the match results, count how many of one/each are used.

If you want only those letters in the string, match_all ^[A-Fa-f]$ and count the results

ppostma1
  • 3,616
  • 1
  • 27
  • 28
  • to do the match_all and counting, see this question here: http://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string – ppostma1 Jun 17 '16 at 01:36