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

151
Visualizações
Javascript replace function is not working as expected with special character

I am trying to update a div tag with new metadata in a file and I am using the replace function to do this job ,but is not working as expected , it is adding junk tag in the middle.

I am trying this below code.

let str = `<div data-bind="style:{'background-color':typeof(CalEventBgColor)!=='undefined'? CalEventBgColor:''},css:typeof(CalEventType)!=='undefined'? 'event '+ CalEventType:'event',attr:{ title:dynamicTitle ,id: CalEventId+'$'+$parentContext.$index()+'$'+$index()+$parents[1].componentid}" role="button" tabindex="0">
</div>`;
let oldVal = `<div data-bind="style:{'background-color':typeof(CalEventBgColor)!=='undefined'? CalEventBgColor:''},css:typeof(CalEventType)!=='undefined'? 'event '+ CalEventType:'event',attr:{ title:dynamicTitle ,id: CalEventId+'$'+$parentContext.$index()+'$'+$index()+$parents[1].componentid}" role="button" tabindex="0">`;
let newVal =`<div :class="[[typeof(CalEventType)!=='undefined'? 'event '+ CalEventType:'event']]" :title="[[dynamicTitle]]" :id="[[CalEventId+'$'+$parentContext.$index()+'$'+$index()+$parents[1].componentid]]" :style="[[{ 'background-color':typeof(CalEventBgColor)!=='undefined'? CalEventBgColor:''}]]" role="button" tabindex="0">`;
console.log(str.replace(oldVal,newVal));

Basically if you see the :id property it is broken with junk character, please let me know if we can do something for this.

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

0

It happens because $ is used to replace substring: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#description

You just have to add one more dollar sign before every dollar. This string CalEventId+'$'+$parentContext.$index()+'$'+$index() should look like this CalEventId+'$$'+$$parentContext.$$index()+'$$'+$index()

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