Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

420
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda