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

185
Vistas
Regex: matches to new line or end of line or space in JS

I wanted to extract :privacy and :date from the example string below.

I wanted to have a regex constraint that describes that a :[^:\s]+ block (e.g. :privacy or :date) can only be ended by a space \s or a newline \n or a end of string $ (so I will be able to have a rule to logically split these blocks in the later steps).

So I simply put (?:$|\n|\s) at the end of the regex, but I doesn't work for me (the 3rd regex below). I double checked that it does work when I separately put \s or $ (the 1st and 2nd regex below), now I have no idea how I can implement the thing. Thanks for your help.

'note::tmp hogehoge. :privacy :date'.match(/\s:[^:\s]+\s/g)
(1) [' :privacy ']

'note::tmp hogehoge. :privacy :date'.match(/\s:[^:\s]+$/g)
(1) [' :date']

'note::tmp hogehoge. :privacy :date'.match(/\s:[^:\s]+(?:$|\n|\s)/g)
(1) [' :privacy ']
about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can use below regex pattern to match a block pattern :[^:\s+]+ ended by a space \s or a newline \n or a end of string $

/((:[^:\s+]+)(?:[\s\n]))|(:[^:\s+]+)(?:[\s\n])?$/gm

(?:[\s\n]) - will check if the block is being followed by a space or a new line
(:[^:\s+]+)(?:[\s\n])?$ - will check if the block is at the end of string or not.

you can also use lookforward technique to achieve the same result

(:[^:\s+]+)(?=\s|\n|$)
about 4 years ago · Santiago Trujillo Denunciar

0

In your pattern \s:[^:\s]+\s you are matching the leading and the trailing whitespace chars.

What you might do is assert a whitespace boundary using (?!\S) to the right.

To get the value without the leading whitespace char, you can use a capture group.

\s(:[^:\s]+)(?!\S)

Regex demo

const s = "note::tmp hogehoge. :privacy :date";
const regex = /\s(:[^:\s]+)(?!\S)/g;
const result = Array.from(s.matchAll(regex), m => m[1]);
console.log(result);

about 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