Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
What is regex for alphanumeric with no spaces?

I need to match this like:

1. 1234           true
2. 1234 5678      false
3. 1234x          true
4. x1234          true
5. abcd           false
6. abcd 1234      false

so I need to match just a string with numbers only or numbers and characters only and with no spaces (single word). This doesn't really work:

/([0-9])\w+/g
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Your pattern ([0-9])\w+ is unanchored, so it can have partial matches.

It also has to start with a digit, and at least 1 word character, have the string to be at least 2 characters long.


You can use anchors, and make sure to match a single digit. For a match only, you can omit the capture group:

^[^\W\d]*\d\w*$
  • ^ Start of string
  • [^\W\d]* Optionally match any word character except a digit
  • \d Match a single digit
  • \w* Match optional word characters
  • $ End of string

Regex demo

Note that \w can also match _

about 4 years ago · Juan Pablo Isaza Report

0

Match the entire string and require a at least one digit:

/^[a-z]*[0-9][a-z0-9]*$/i

See regex proof. Important: i flag (case insensitive).

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  [a-z]*                   any character of: 'a' to 'z', 'A' to 'Z' (0 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  [0-9]                    any character of: '0' to '9'
--------------------------------------------------------------------------------
  [a-z0-9]*                any character of: 'a' to 'z', 'A' to 'Z', '0' to '9'
                           (0 or more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  $                        the end of the string
about 4 years ago · Juan Pablo Isaza Report

0

const strings = ['1234','12x3', '12 X'];
console.log(strings.filter(string=> string.match(/^[0-9a-z]+$/i)));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!