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

418
Views
RegEx: If first character is '%' or alphanumeric then

in nodejs, I have to following pattern

(/^(>|<|>=|<=|!=|%)?[a-z0-9 ]+?(%)*$/i

to match only alphanumeric strings, with optional suffix and prefix with some special characters. And its working just fine.

Now I want to match the last '%' only if the first character is alphanumeric (case insensitive) or also a %. Then its optionally allowed, otherwise it should not match.

Example:

Should match:

>test
!=test
<test
>=test
<=test
%test
test
%test%
test%

Example which should not match:

<test% <-- its now matching, which is not correct
<test< <-- its now **not** matching, which is correct

Any Ideas?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can add a negative lookahead after ^ like

/^(?![^a-z\d%].*%$)(?:[><]=?|!=|%)?[a-z\d ]+%*$/i
  ^^^^^^^^^^^^^^^^^  

See the regex demo. Details:

  • ^ - start of string
  • (?![^a-z\d%].*%$) - fail the match if there is a char other than alphanumeric or % at the start and % at the end
  • (?:[><]=?|!=|%)? - optionally match <, >, <=, >=, != or %
  • [a-z\d ]+ - one or more alphanumeric or space chars
  • %* - zero or more % chars
  • $ - end of string
over 4 years ago · Santiago Trujillo Report

0

You might use an alternation | to match either one of the options.

^(?:[a-z0-9%](?:[a-z0-9 ]*%)?|(?:[<>]=?|!=|%)?[a-z0-9 ]+)$
  • ^ Start of string
  • (?: Non capture group
    • [a-z0-9%] Match one of the listed in the character class
    • (?:[a-z0-9 ]*%)? Optionally match repeating 0+ times any of the character class followed by %
    • | Or
    • (?:[<>]=?|!=|%)? Optionally match one of the alternatives
    • [a-z0-9 ]+ Match 1+ times any of the character class
  • ) Close non capture group
  • $ End of string

Regex demo

over 4 years ago · Santiago Trujillo 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!