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

159
Vistas
Javascript Regex match a line that ends with char ' when not preceeded by?

I'm trying to create a regex that will match all lines with certain PREFIX and the match should end when a char ' is found. However, if there is a ? before the ', then it should not count it as the end of the line. Basically the question mark ? serves as a escape char.

This is what I have been using, but it does not work for what i'm trying to do: This is the regex /SOME_PREFIX+[^']+/

[EDIT] This is the full expected text:

you could have something before'SOME_PREFIX foo bar'something in the middle'SOME_PREFIX BAR FOO ?'SHOULD ALSO MATCH'test'

SOME_PREFIX foo bar' <---Should be a match 1

SOME_PREFIX BAR FOO ?'SHOULD ALSO MATCH' <--- Should be a match 2, but this line does not work, since it only matches up until ?' which is the first ocurrence of the '

For reference: https://regexr.com/63io9

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could use, for example:

const text = "you could have something before'SOME_PREFIX foo bar'something in the middle'SOME_PREFIX BAR FOO ?'SHOULD ALSO MATCH'test'";

const matches = text.match(/SOME_PREFIX(?:[^'?]+|\?'?)*'/g);

console.log(matches);

[^'?]+ matches any character that is not a ' or ?, one or more times, and \?'? means match a ? optionally follwed by a '.

An alternation construct such as (?:x|y) means try and match x first and if it doesn't match, then try and match y.

about 4 years ago · Juan Pablo Isaza Denunciar

0

If a lookbehind is supported in your environment, you could match SOME_PREFIX, and then match any char except SOME_PREFIX or a ' and only match the ' when it it directly preceded by ?

SOME_PREFIX(?:(?!SOME_PREFIX|').)*(?:(?<=\?)'(?:(?!SOME_PREFIX|').)*)*'

Explanation

  • SOME_PREFIX Match literally
  • (?: Non capture group to repeat as a whole
    • (?!SOME_PREFIX|') Negative lookahead to assert not SOME_PREFIX or ' directly to the right
    • . Match any char except a newline to not cross lines
  • )* Close the non capture group and optionally repeat it
  • (?: Non capture group to repeat as a whole
    • (?<=\?)' Match ' only when directly preceded by ?
    • (?:(?!SOME_PREFIX|').)* The same repeating pattern as in the first part
  • )* Close the non capture group and optionally repeat it
  • ' Match a single quote

Regex demo

const regex = /SOME_PREFIX(?:(?!SOME_PREFIX|').)*(?:(?<=\?)'(?:(?!SOME_PREFIX|').)*)*'/g;
const str = "you could have something before'SOME_PREFIX foo bar'something in the middle'SOME_PREFIX BAR FOO ?'SHOULD ALSO MATCH'test'";
console.log(str.match(regex));

about 4 years ago · Juan Pablo Isaza Denunciar

0

You may use

^(?!.*\?'$)SOME_PREFIX.+'

See a demo on regex101.com.

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