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

168
Vistas
Possible ways to emulate \A and \z meta escapes (string anchors)

I've found that \A can be emulated with:

(?<!\s)^

and PCRE(2)/re2 \z (Python \Z) can be emulated with:

$(?!\s)

regex101 demo

How else can \A and/or \z be emulated in JS?

Even regular-expression.info has no mention of what I've presented.

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

0

Apart from the lookarounds that you have used, you can try one of the following:

Disable the multiline modifier. InJavascript, by default, ^ matches \A and $ matches \z and use:

^|$

Demo

or

you can capture them in groups as shown below:

(^)[\s\S]*($)

Demo


  • (^) - match the start of the first line and capture it in group 1
  • [\s\S]* - greedily match 0+ occurences of any character(including newlines)
  • ($) - match the end of the last line and capture it in group 2
about 4 years ago · Juan Pablo Isaza Denunciar

0

When you have no need of ^ and $ as line-boundaries, then these symbols act like \A and \z by default. Demo:

let results = 
`a good test with
a second line, and ending with a bad z
a third line, and a bad z
a final line, and a good z`
.match(/^a (\w+)|(\w+) z$/g);

console.log(results);

If you do need to use line-boundary detection in your regex, then you would normally use the /m modifier, and then you can use your idea of look-around.

Alternatively, you could leave out the /m modifier, and just use ^ and $ for \A and \z, but then use look-around for detecting the line boundaries:

  • start-of-line: (?<![^\n\r])
  • end-of-line: (?![^\n\r])

let results = 
`a bad test with
a good line, and ending with a good z
a third line, after a good line, and a bad z
a final line, and a bad z`
.match(/(?<![^\n\r]). good|good .(?![^\n\r])/g);

console.log(results);

For specific cases you can sometimes use other techniques. For instance, by default (so without /s) the pattern .* will capture anything until the end of the line, so you can be sure to be at a line-end after that capture.

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