admin管理员组文章数量:1130349
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 that0characters would be selected. – zzzzBov Commented Nov 7, 2012 at 17:43
2 Answers
Reset to default 1Your 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
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 that0characters would be selected. – zzzzBov Commented Nov 7, 2012 at 17:43
2 Answers
Reset to default 1Your 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
本文标签:
版权声明:本文标题:javascript - Regular Expression: Letters, numbers, spaces, hyphens, periods, and underscores only and must begin with a letter - 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1745278013a2143208.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论