0

I'm trying to check if a string contains specific letters like E or F, with the following code

/^(F?)/.test('E22')

The problem is that it returns true, when it should return false. And what bothers me most, is that testing the regex at regexpal.com, goes perfect, but when applied... wrong.

Any idea what goes wrong?

http://jsfiddle.net/alleks/CykQv/

UPDATE

I have explained my self wrong. I do individual checks, in different cases. So, in specific cases I need to see if the string contains an E, and in others, if contains an F

//Case1
if (/^(F?)/.test(stringContainsE)) ....

//Case2
if (/^(F?)/.test(stringContainsF)) ....

Update2

Both cases return TRUE when they shouldn't: http://jsfiddle.net/alleks/CykQv/2/

Alex
  • 7,538
  • 23
  • 84
  • 152
  • You have to read about regexps (see http://www.regular-expressions.info/). In the meantime, try with `/[EF]/g`. – sp00m Nov 04 '13 at 16:55
  • Do the E and F have to occur at the beginning? – Mike Edwards Nov 04 '13 at 16:59
  • possible duplicate of [Regular expression to match string not containing a word?](http://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-word) – mwhs Nov 04 '13 at 17:01
  • @MikeEdwards Yea. The strings are `E**` or `F**` – Alex Nov 04 '13 at 17:01
  • lol, not sure why you even want regex. You could just access the first character using something like this [`str = "test"; alert(str[0]);`](http://jsfiddle.net/kPUAP/). After that it's just a matter of adding conditionals. – HamZa Nov 04 '13 at 17:01
  • 2
    With regard to your second edit, you still are using a question mark. Please read my updates below. You don't need the capturing parentheses or the '?' to simply test for a single character at the beginning of a string. – Mike Edwards Nov 04 '13 at 17:06
  • @w0rldart: if you're testing whether the strings have a given letter at the start, then "contain" is the wrong word. "Contains F" means "Has an F somewhere inside it". You mean "starts with F". – Paul D. Waite Nov 04 '13 at 17:09
  • @MikeEdwards My 2nd edit, was before seeing your answer... – Alex Nov 04 '13 at 17:09
  • 1
    @PaulD.Waite Thanks for the point! I have now a better notion on how to express my self, in such cases. – Alex Nov 04 '13 at 17:10

2 Answers2

9

The question mark makes the F optional. That regex will return true for every single string.

/[EF]/

Will match strings that contain a letter E and/or a letter F.

EDIT: As Paul mentioned in his comment, the ^ character matches the beginning of the string. If the character must occur at the beginning of the string then:

/^E/

will test for an E at the beginning of the string. Simply omit the ^ if you want anywhere in the string. However, in Javascript in this case you should simply use:

myString.charAt(0) === 'E' // for the beginning or
myString.indexOf('E') !== -1 // for anywhere

A regex for this simple operation is overkill.

Mike Edwards
  • 3,742
  • 17
  • 23
  • 1
    Note that the original regex starts with the `^` character, which anchors matches at the start of the string. To match an `E` or an `F` at the start of the string, you'd do `/^[EF]/`. – Paul D. Waite Nov 04 '13 at 16:57
  • I have miss explained my self. I don't need to check if contains `E` or `F` in same string – Alex Nov 04 '13 at 17:00
  • @w0rldart - do you mean it contains `E` and `F` in the same string? `or` would only require one of them in the string. `and` would require both to be present in the same string. Which do you want? – jfriend00 Nov 04 '13 at 17:02
5

I'm trying to check if a string contains specific letters like E or F

Regex should be:

/[EF]/.test('E22');

Your regex ^F? makes F as optional which will always return true for any input.

UPDATE: This should work without optional anchor ?

//Case1
if (/^E/.test(stringContainsE)) ....

//Case2
if (/^F/.test(stringContainsF)) ....
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • You have miss understood my question. I am trying to figure it out if the string contains an `E` or `F` in different cases... and never, both letters in the same string. – Alex Nov 04 '13 at 17:02