开发者

How do I write a regex that allows for up to 7 characters plus one whitespace? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 9 years ago.

How do I write a re开发者_C百科gex that allows for up to 7 characters plus one whitespace

Sorry I'm a complete novice at regex stuff

Any help would be greatly appreciated


MODIFIED: The character can be either a letter or number...as it's for a postcode widget


I would think

/^\w{1,7}\s?$/

which is

^       start of string  
\w      letters, numbers and underscore  
{1,7}   one to seven  
\s      whitespace  
?       zero or 1 of the preceding  - remove it if you want JUST one space  
$       end of string

If you do not want the underscore change \w to [a-zA-Z0-9]
If you want ANY character, change \w to a . (fullstop)

More here http://lawrence.ecorp.net/inet/samples/regexp-intro.php


This should match what you need:

\S{0,7}\s

Unfortunately you didn't specify what kind of characters you need to match. So this one matches up to seven non-whitespace characters followed by a whitespace character.


Try this regex:

/^[A-Za-z0-9]{0,7}\s$/


/[a-zA-Z0-9]{0,7}\s/

7 chars followed by one whitespace character.


  • Exactly one whitespace: /^.{0,7}\s$/
  • At least one whitespace: /^.{0,7}\s+$/
  • Zero or one whitespace: /^.{0,7}\s?$/
  • Zero or more whitspace: /^.{0,7}\s*$/

If you want to require at least one character before the whitespace, use {1,7}.


Something like this should help you:

[a-zA-Z0-9]{1,7}\s
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜