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

172
Views
matching regex pattern in javascript using RegExp

I have a string from which I want to check if it only contains characters that are allowed. I want only characters of

  • alphanumeric (lower & upper) : A-Z, a-z

  • numeric (0-9)

  • special characters: _&!.-

  • space

example of valid strings:

  • title1 - header
  • title2&&& header_!.
  • 09
  • a912
  • ...dd

example of invalid strings

  • a$$$ ()
  • a12345^&6**

if at least one of characters is not in the special characters so it not valid string. I would like to know how do test it in javascript. the regex patterns come from server so it string. I convert the regex pattern string by using RegExp but all of them show false, even some of them are valid. the three first lines should be valid (true)

 const pattern = "^[a-z0-9_&!.-\\s]+$";

                const strings = [
                  `title1 - header`,
                  `title2&&& header_!.-`,
                  `09`,
                  `a912 dd`,
                  `a$$$ ()`,
                  `a12345^&6**`
                ]
                const regex = new RegExp(pattern);

                strings.forEach(str => {

                  console.log(str, ' ---> ', regex.test(str));
                });

about 4 years ago · Juan Pablo Isaza
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!