Regex to match string containing letters and only underscore

Well my question is simple, I want to match a string with following attributes

  • No white space
  • Must start with a letter
  • Must not contain any other special characters other than underscore
  • May contain numbers

Please help in creating such a regex.

Answers


^[a-zA-Z][a-zA-Z0-9_]*$

Dissecting it:

  • ^ start of line/string
  • [a-zA-Z] starts with a letter
  • [a-zA-Z0-9_]* followed by zero or more letters, underscores or digits.
  • $ end of line/string

If you need to consider Unicode, then the following is probably more sane:

^\p{L}[\p{L}\p{Nd}_]*$

This will match not only ASCII letters and digits but across all scripts that are supported by Unicode. Digits are restricted to decimal digits, only, so you won't get Roman numerals.


Need Your Help

jQuery looks/works good in Firefox but not IE (Internet Explorer)

javascript jquery drupal drupal-modules jcarousel

I have jCarousel 0.2.8 working with jQuery 1.7.1 and jQuery UI 1.8.11 in my Drupal 7 module perfectly in Firefox. However, the carousels are not working properly in Internet Explorer 7, 8, 9 and 10...

Regex (Ruby) to capture object name and id number

html ruby regex

For example I have two potential markups:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.