Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
How to reuse matched value of regex in JS replace() function in the function itself?

Let's say I have a string, I want to get a match in some string. To find it, I use regex, and then replace it. However, how would I replace it with the match itself but edited?

I'm having a hard time using match() and matchAll() because the input text is not necessarily a string and the outputs of the match functions aren't making sense at all in the context I'm using it in. They return as RegExpStringIterator(s).

Purpose:

Trying to make a google extension that edits content on the page.

Ex.

regex = /123/g
let text = "XYZ string is some string is a string, but 123 is what I want to change."
let edit = text.replace(regex, numGen(***not sure what to pass here***))

console.log(edit)

function numGen(match) {
//Here I want to get the match and add the string "4" to it
}

"""
Expected outcome of "console.log(edit)": 
XYZ string is some string is a string, but ***1234*** is what I want to change.
"""

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Based on your example, if you only need to append string/s (e.g. text) to the matched result, you can simply use replace():

const regex = /123/g;
const text = "XYZ string is some string is a string, but 123 is what I want to change.";
const edit = text.replace(regex, '***$&***');
// $& means the whole matched string
// result: XYZ string is some string is a string, but ***123*** is what I want to change.

If you need to run some code to change the matched result, then you you can use a replacerFunction:

const regex = /123/g;
const text = "XYZ string is some string is a string, but 123 is what I want to change.";
const edit = text.replace(regex, function(m) {
  // m here means the whole matched string
  // for example, let's double the number
  return m * 2;
  // resulat: XYZ string is some string is a string, but 246 is what I want to change.
});

Same as above but using arrow function:

const regex = /123/g;
const text = "XYZ string is some string is a string, but 123 is what I want to change.";
const edit = text.replace(regex, m => {
  // m here means the whole matched string
  // for example, let's double the number
  return m * 2;
  // result: XYZ string is some string is a string, but 246 is what I want to change.
});

Note: For a single operation, above can also be written as:

const edit = text.replace(regex, m => m * 2);
about 4 years ago · Juan Pablo Isaza Relatório

0

Do you mean this? Check inline comments

UPD with calling external function on match

// External function to call with each match
const numGen = match => {
  // Here you get the match as argument from replace function
  // Then add the string "4" to it
  match += "4";
  // Then return final result back to replace
  return match;
}

// Replace method part
const regex = /string/g;
let counter = 0;
let text = "XYZ string is some string is a string.";

// Just set your external function as replace parameter.
// Matched string will be passed to it as arguments
let edit = text.replace(regex, numGen);

// Result
console.log(edit);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda