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 9256773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:01:50+00:00 2026-06-18T12:01:50+00:00

Regular expression for no white space at start or end, but allow white space

  • 0

Regular expression for no white space at start or end, but allow white space in middle, but also allow only one char inputs:

The closest I have got is : ^([^\s])([\sa-zA-Z0-9_\-]*)([^\s])$

The problem with this is that it will only pass if the input is three or more chars, I need it to accept a single char and pass. EG:

The following should pass (using ” for easy of reading, not required in string)

"A"
"A B"
"Hello There"

The following should fail

" A"
"A "
" A "
" test"

also needs to only allow A-Z a-z 0-9 – _ though out, with addition of spaces in middle section but not at start or end

Any ideas?

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-06-18T12:01:52+00:00Added an answer on June 18, 2026 at 12:01 pm

    Non-spaces, optionally followed by multiple groups of spaces then non-spaces:

    ^[^\s]+(\s+[^\s]+)*$
    

    Edit:

    To include the character restrictions, just replace non-space classes with allowable characters:

    ^[-_a-zA-Z0-9]+(\s+[-_a-zA-Z0-9]+)*$
    

    Also, consider being Unicode friendly and using something like

    ^[-_\p{Alnum}]+(\s+[-_\p{Alnum}]+)*$
    

    (assuming your regex interpreter allows it).

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

Sidebar

Related Questions

I am looking for a regular expression for matching that contains no white space
I am using this regular expression to remove white space and line breaks from
Do I need to escape a single white space in a regular expression? I
Regular expressions can become quite complex. The lack of white space makes them difficult
I am using Perl Regular expressions. How would i go about ignoring white space
How can I write a regular expression in javascript that only allows users to
Can a regular expression match whitespace or the start of a string? I'm trying
The regular expression ^[A-Za-z](\W|\w)* matches when the user gives the first letter as white
I am trying to get one regular expression that does the following: makes sure
I have a regular expression that allows only specific characters from the name fields

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.