javascript - Regular Expression: Letters, numbers, spaces, hyphens, periods, and underscores only and must begin with a letter -

After a few tutorials I thought I had this, but nope ---I am trying to only match a string of letters,

After a few tutorials I thought I had this, but nope ---

I am trying to only match a string of letters, numbers, spaces, hyphens, periods, and underscores only and must begin with a letter in a jquery function

This is what I came up with after the tutorials but it only accepts letters:

/^[a-z][a-z0-9_ .-]*?/i

Please help!

++EDITED++

This is the code where I am trying to use this:

$('input[name=albumName]').keyfilter(/^[a-z][a-z0-9_ .-]*?/i);

The name that a user is entering will also be used in a URL so I want to limit their entry. I would want this to be allowed ( Texas AandM - 2012 ) but this not to be allowed ( 2012 - Texas A&M )

After a few tutorials I thought I had this, but nope ---

I am trying to only match a string of letters, numbers, spaces, hyphens, periods, and underscores only and must begin with a letter in a jquery function

This is what I came up with after the tutorials but it only accepts letters:

/^[a-z][a-z0-9_ .-]*?/i

Please help!

++EDITED++

This is the code where I am trying to use this:

$('input[name=albumName]').keyfilter(/^[a-z][a-z0-9_ .-]*?/i);

The name that a user is entering will also be used in a URL so I want to limit their entry. I would want this to be allowed ( Texas AandM - 2012 ) but this not to be allowed ( 2012 - Texas A&M )

Share Improve this question edited Nov 7, 2012 at 17:51 raina77ow 107k16 gold badges204 silver badges236 bronze badges asked Nov 7, 2012 at 17:32 jgravoisjgravois 2,57910 gold badges44 silver badges67 bronze badges 12
  • 1 Which language or tool are you using? And you should also add a $ at the end, otherwise you allow any characters after the beginning letter. – Martin Ender Commented Nov 7, 2012 at 17:34
  • sorry, didn't realize language made a difference. This is a jquery function to limit values entered into a textbox – jgravois Commented Nov 7, 2012 at 17:37
  • 3 it makes a great difference for certain problems. please include your code that uses the regex and a few example strings that fail to behave as you would like them to. – Martin Ender Commented Nov 7, 2012 at 17:39
  • You might need to escape the - – Ed Heal Commented Nov 7, 2012 at 17:41
  • 2 If the entire string should match, you should use $ instead of ?: /^[a-z][a-z0-9_ .-]*$/i. The ? forces the * to select minimally, and given its location in the match would mean that 0 characters would be selected. – zzzzBov Commented Nov 7, 2012 at 17:43
 |  Show 7 more ments

2 Answers 2

Reset to default 1

Your regex looks ok to me. Perhaps there is a problem with the surrounding code rather than the regex? This is a Python example:

s_re = re.pile('^[a-z][a-z0-9_.-]*',re.I) # case insensitive match

In [12]: if s_re.match('A'): print 'match'
match

In [14]: if s_re.match('A.-'): print 'match'
match

In [15]: if s_re.match('1.-'): print 'match'

In [16]: if s_re.match('1_.-'): print 'match'

In [17]: if s_re.match('A_.-'): print 'match'
match

If you want to make sure you want at least one character after the first letter, you can replace the * with a +, or {2,} with at least 2 more characters, or {2,5} with between 2 and 5 characters.

^[a-z][a-z0-9_ .-]* works.

Because of the Question Mark, the Regex matched only letters, because the match is the smallest possible match.

Reference/RegExp

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信