最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex to allow letters and numbers but not numbers alone - Stack Overflow

programmeradmin1浏览0评论

I'm trying to write a regex for javascript which will allow letters and numbers but won't allow numbers alone.

So '12 Test Street' would validate, as would 'test street' but not '12'.

Not that familiar with regexs so I've no idea where to start. I managed to write:

^([A-Za-z\d\s]+[A-Za-z\s])+$

That does work to a point but if a space is then added to the end of the numbers, it will validate again.

I'm trying to write a regex for javascript which will allow letters and numbers but won't allow numbers alone.

So '12 Test Street' would validate, as would 'test street' but not '12'.

Not that familiar with regexs so I've no idea where to start. I managed to write:

^([A-Za-z\d\s]+[A-Za-z\s])+$

That does work to a point but if a space is then added to the end of the numbers, it will validate again.

Share Improve this question asked Jul 2, 2014 at 16:01 Alex FoxleighAlex Foxleigh 1,9742 gold badges22 silver badges52 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 8

You can solve this easily with a negative look-ahead:

^(?!\d+$)[a-zA-Z\d\s]+$

Note that this allows space-only strings. I'll leave it as a small exercise to change the expression if that's not desired. :)

If I interpret almost strictly (you did not mention whitespace characters) your question I suppose this will work:

^\d*[a-zA-Z][a-zA-Z\d\s]*$
发布评论

评论列表(0)

  1. 暂无评论