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

155
Vistas
Replace everything except a matching pattern in regex

I'm attempting to delete everything but a matching pattern using javascripts .replace() function and regex.

I want to save the digits from the "date" field, i.e 11 10 2021 or 9 10 2021. This regex /[\d]{1,2} [0-9]{2} [0-9]{4}/g matches to the date patterns, however when using .replace(regex, '') it replaces those digits rather than saving them. I'm just wondering how to "invert" the pattern to save the digits and replace everything else.

Example strings:

SURVEY API RESULT LANDING: [{"_id":"616392e41a03eed562de2e8a",
"userId":"email@gmail.com",
"date":"11 10 2021",
"timeStamp":"2:26:",
"formResponse":"Survey completed"}]
    
[{"_id":"616392e41a03eed562de2e8a",
"userId":"email@gmail.com",
"date":"9 10 2021",
"timeStamp":"2:26:",
"formResponse":"Survey completed"}]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Use regex exec or string matchAll instead string replace

exec

const regex = /[\d]{1,2} [\d]{2} [\d]{4}/g;
const str = `SURVEY API RESULT LANDING: [{"_id":"616392e41a03eed562de2e8a",
"userId":"email@gmail.com",
"date":"11 10 2021",
"timeStamp":"2:26:",
"formResponse":"Survey completed"}]
    
[{"_id":"616392e41a03eed562de2e8a",
"userId":"email@gmail.com",
"date":"9 10 2021",
"timeStamp":"2:26:",
"formResponse":"Survey completed"}]`;

let match;

while(match = regex.exec(str)) {
    console.log(match[0]);
}

matchAll

const iterator = `SURVEY API RESULT LANDING: [{"_id":"616392e41a03eed562de2e8a",
"userId":"email@gmail.com",
"date":"12 10 2021",
"timeStamp":"2:26:",
"formResponse":"Survey completed"}]
    
[{"_id":"616392e41a03eed562de2e8a",
"userId":"email@gmail.com",
"date":"9 10 2021",
"timeStamp":"2:26:",
"formResponse":"Survey completed"}]`.matchAll(/[\d]{1,2} [\d]{2} [\d]{4}/g);

console.log(Array.from(iterator).map(m => m[0]));

about 4 years ago · Juan Pablo Isaza Denunciar

0

To find a single date, try the following

`[{"_id":"616392e41a03eed562de2e8a",
"userId":"email@gmail.com",
"date":"9 10 2021",
"timeStamp":"2:26:",
"formResponse":"Survey completed"}]`.replace(/.*([\d]{1,2} [0-9]{2} [0-9]{4}).*/gms, "$1");

RegExp reference https://javascript.info/regexp-introduction

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