2

I had a working regex for date until I tested it in the iOS simulator. Apparently, iOS requires a month name (ex: Jan), two digit date and four digit number.

I'm new to regular expressions but here was my attempt, it was unsuccessful.

/^(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s\d{2},\d{4}$/;
4

2 に答える 2

2

/^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/mm/dd/yyyy でこれを試してください

および dd/mmm/yyyy の場合

^(([0-9])|([0-2][0-9])|([3][0-]))\/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\/\d{4}$

于 2012-11-09T15:49:40.443 に答える
1

式は次のような文字列を受け入れます。

jan 12,2012 // works
Jan 12,2012 // doesn't work!

大文字の J に注意してください。正規表現では大文字と小文字が区別されます。おそらく、それが機能しない理由です。

次のようなことを試してください:

/^((j|J)an|(f|F)eb|(m|M)ar|(a|A)pr|(m|M)ay|(j|J)un|(j|J)ul|(a|A)ug|(s|S)ep|(o|O)ct|(n|N)ov|(d|D)ec)\s\d{2},\d{4}$/

月を定義する必要がありますか?もっと柔軟性がある場合は、これを試してください:

/^[a-zA-Z]{3}\s\d{2},\d{4}$/
/[a-zA-Z]{3}\s\d{2},\d{4}/     // works also

これは、正規表現をテストするために使用する良いサイトです: RegexPal

あいさつ

于 2012-11-09T16:04:17.913 に答える