javascript - Regex if string contains E or F letter - Stack Overflow

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

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, goes perfect, but when applied... wrong.

Any idea what goes wrong?

/

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: /

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., goes perfect, but when applied... wrong.

Any idea what goes wrong?

http://jsfiddle/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/alleks/CykQv/2/

Share Improve this question edited Nov 4, 2013 at 17:05 Alex asked Nov 4, 2013 at 16:52 AlexAlex 7,68825 gold badges86 silver badges153 bronze badges 9
  • You have to read about regexps (see regular-expressions.info). In the meantime, try with /[EF]/g. – sp00m Commented Nov 4, 2013 at 16:55
  • Do the E and F have to occur at the beginning? – Mike Edwards Commented Nov 4, 2013 at 16:59
  • possible duplicate of Regular expression to match string not containing a word? – mwhs Commented Nov 4, 2013 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 Commented Nov 4, 2013 at 17:06
  • 1 @PaulD.Waite Thanks for the point! I have now a better notion on how to express my self, in such cases. – Alex Commented Nov 4, 2013 at 17:10
 |  Show 4 more ments

2 Answers 2

Reset to default 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 ment, 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.

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)) ....

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742399400a4436603.html

相关推荐

  • javascript - Regex if string contains E or F letter - Stack Overflow

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

    2天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信