Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 683163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:40:37+00:00 2026-05-14T01:40:37+00:00

I want a regular expression to match valid input into a Tags input field

  • 0

I want a regular expression to match valid input into a Tags input field with the following properties:

  • 1-5 tags
  • Each tag is 1-30 characters long
  • Valid tag characters are [a-zA-Z0-9-]
  • input and tags can be separated by any amount of whitespace

For example:

Valid: tag1 tag2 tag3-with-dashes tag4-with-more-dashes tAaG5-with-MIXED-case

Here’s what I have so far–it seems to work but I’m interested how it could be simplified or if it has any major flaws:

\s*[a-zA-Z0-9-]{1,30}(\s+[a-zA-Z0-9-]{1,30}){0,4}\s*

// that is: 
\s*                          // match all beginning whitespace
[a-zA-Z0-9-]{1,30}           // match the first tag
(\s+[a-zA-Z0-9-]{1,30}){0,4} // match all subsequent tags
\s*                          // match all ending whitespace

Preprocessing the input to make the whitespace issue easier isn’t an option (e.g. trimming or adding a space).

If it matters, this will be used in javascript. Any suggestions would be appreciated, thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:40:37+00:00Added an answer on May 14, 2026 at 1:40 am

    You can simplify it a bit like this:

    ^(?:(?:^|\s+)[a-zA-Z0-9-]{1,30}){1,5}\s*$
    

    The (?: ) syntax is a noncapturing group, which I believe should improve performance when you don’t need groups per se.

    Then the trick is this statement:

    (?:^|\s+)
    

    Thanks to the caret, this will match the beginning of the line, or one or more characters of whitespace.

    UPDATE: This works perfectly in my testing and there’s certainly less redundant code. However, I just used the benchmarking in Regex Hero to find that your original regex is actually faster. That’s probably because mine is causing more backtracking to occur.

    UPDATE #2: I found another way that accomplishes the same thing, I think:

    ^(?:\s*[a-zA-Z0-9-]{1,30}){1,5}\s*$
    

    I realized that I was trying too hard. \s* matches 0 or more spaces, which means that it’ll work for a single tag. But… it’ll work for 2-5 tags as well because the space is not in your character class [ ]. And indeed it fails with 6 tags as it should. That means this a much more forward-looking regex with less backtracking, better performance, and less redundancy.

    UPDATE #3:

    I see the error in my ways. This should work better.

    ^(?:\s*[a-zA-Z0-9-]{1,30}\b){1,5}\s*$
    

    Putting the \b just before the last ) will assert a word boundary. That allows the 1-30 character length rule to work properly again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The problem: dictionary.XPathSelectElements("group[field[@name='StipulationType']]") This selects all the group elements satisfying… May 14, 2026 at 10:26 pm
  • Editorial Team
    Editorial Team added an answer First, I think you need to be using the GridView… May 14, 2026 at 10:26 pm
  • Editorial Team
    Editorial Team added an answer You can download Carmack's source for Wolfenstein3D for the iphone… May 14, 2026 at 10:26 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.