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

266
Visualizações
BigQuery using Regex in Javascript UDF escaping dynamic value

Im trying to use regex with dynamic variable in BigQuety with JS UDF but there is some problem with the escaping and I don't know how to solve it.

CREATE TEMP FUNCTION `replaceRegex`(org STRING, to_replace STRING, replace_to STRING)
RETURNS STRING LANGUAGE js AS
"""
    re_string = `\\b${to_replace}\\b`
    let re = new RegExp(re_string, 'gi');
    return org.replace(re, replace_to);
""";

SELECT replaceRegex('The quick brown fox jumps over the lazy dog', 'fox', 'bear')

This version does not work.

But I change: \\b${to_replace}\\b to:

re_string = /\\bfox\\b/

Then it does work.

But I cant hardcode my regex it has to be dynamic value passed.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Adding r at the start of your triple quoted string makes your code work as it treats everything as raw strings see this reference. Your code should be:

CREATE TEMP FUNCTION `replaceRegex`(org STRING, to_replace STRING, replace_to STRING)
RETURNS STRING LANGUAGE js AS
r"""
    re_string = `\\b${to_replace}\\b`
    let re = new RegExp(re_string, 'gi');
    return org.replace(re, replace_to);
""";

SELECT replaceRegex('The quick brown fox jumps over the lazy dog', 'fox', 'bear')

Test:

enter image description here

about 4 years ago · Santiago Trujillo 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