2
I need the ' ' (Reverse Solidus) character to be included with the captured group value.
Example:
In the sentence Assets/pdf/regulation_demais_ddds_ Hi Mod.pdf, I need the stretch
be replaced by
\/pdf\/
forming the phrase
assets\/pdf\/regulamento_demais_ddds_Oi_Mod.pdf
Regular expression working: https://regexr.com/3nr2m
Example Code:
//Expressão regular aplicada
(\/(?=pdf|images)pdf|\/images)
//Texto para achar o grupo de substituição
const texto = 'assets/pdf/regulamento_Oi_Mod.pdf assets/images/oi-mod-tela-controle-2x.gif'
const resultado = texto.replace(/(\/(?=pdf|images)pdf|\/images)/g, '/\$1\/');//Replace para incluir '\' no valor capturado
The problem is that the character ' ' is not recognized.
Code:
//Texto para achar o grupo de substituição
const texto = 'assets/pdf/regulamento_Oi_Mod.pdf assets/images/oi-mod-tela-controle-2x.gif'
//Replace para incluir '\' no valor capturado
const resultado = texto.replace(/(\/(?=pdf|images)pdf|\/images)/g, '/\$1\/');
console.log(resultado)
Use
texto.replace(/(\/(?=pdf|images)pdf|\/images)/g, '\\$1\\');
– Valdeir Psr
One demo on Regex101
– danieltakeshi