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

402
Views
How to make regex expression for alphanumeric and some special characters?

I'm using regular expression -

/^[0-9a-zA-Z ~!@#$%^&*-_+=|?]*$/ 

to accept alphanumeric and some special characters for the string validation in JS. But, I don't want all string characters to be special characters only. Please suggest a valid regular expression for this case.

Thank you.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could express your regex as:

^[0-9a-zA-Z ~!@#$%^&*-_+=|?]*[0-9a-zA-Z][0-9a-zA-Z ~!@#$%^&*-_+=|?]*$

This says to match at least one pure alphanumeric character, with optional other alphas or symbol characters.

^                             start of the string
[0-9a-zA-Z ~!@#$%^&*-_+=|?]*  match zero or more alphas or symbols
[0-9a-zA-Z]                   match one alphanumeric character
[0-9a-zA-Z ~!@#$%^&*-_+=|?]*  match zero or more alphas or symbols
$                             end of the string

For a slightly more terse regex, you could also use a positive lookahead:

^(?=.*[0-9a-zA-Z])[0-9a-zA-Z ~!@#$%^&*-_+=|?]*$
about 4 years ago · Juan Pablo Isaza Report

0

You can do it in several ways.

  • "any number of alphanumeric or special" followed by "alphanumeric" followed by "any number of alphanumeric or special" (asserts that there is at least one alphanumeric in there)

    ^[0-9a-zA-Z ~!@#$%^&*-_+=|?]*[0-9a-zA-Z][0-9a-zA-Z ~!@#$%^&*-_+=|?]*$
    
  • assert at the start "lookahead: any number of characters later, there will be an alphanumeric"

    ^(?=.*[0-9a-zA-Z])[0-9a-zA-Z ~!@#$%^&*-_+=|?]*$
    
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!