How to replace with ' ' ' character with Regexp?

Asked

Viewed 721 times

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

/pdf

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)
     

4 answers

3


var texto = 'assets/pdf/regulamento_Oi_Mod.pdf assets/images/oi-mod-tela-controle-2x.gif';

var mapObj = {
   "/pdf/":"\\/pdf\\/",
   "/images/":"\\/images\\/"
};

var re = new RegExp(Object.keys(mapObj).join("|"),"gi");
texto = texto.replace(re, function(matched){
  return mapObj[matched];
});

console.log(texto);

That way I think it’s easier to include more occurrences.

Just include more occurrences in var mapObj

Example:

var texto = 'assets/pdf/regulamento_Oi_Mod.pdf assets/images/blabla /mais ocorrencias/oi-mod-tela-controle-2x.gif';

 var mapObj = {
   "/pdf/":"\\/pdf\\/",
   "/images/":"\\/images\\/",
   "/mais ocorrencias/":"\\/mais ocorrencias\\/"
};

var re = new RegExp(Object.keys(mapObj).join("|"),"gi");
texto = texto.replace(re, function(matched){
  return mapObj[matched];
});

console.log(texto);

  • Really gets practical.

  • Solved me another problem that I would start searching now. It turned out really good. Thanks @Leocaracciolo!

1

Hello, to include the " " character just use the replace method of the String object, as follows:

var texto = 'assets/pdf/regulamento_Oi_Mod.pdf assets/images/oi-mod-tela-controle-2x.gif';
texto = texto.replace(/\//g, '\\/');
console.log(texto);

The above code will override every occurrence of the characters "/" for "\/".

  • But only in the occurrences of pdf and images.

  • 2

    Thank you Diego, but I needed it to be only in the occurrences of pdf and images as @dvd mentioned.

1

You need to escape the inverted bars on replace with another backslash:

'\\$1\\'
 ↑   ↑
barras invertidas de escape

//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)

  • 1

    I’ll tell you, that question is a bitch!! :)

  • good joke, @Leocaracciolo! hahaha

  • 2

    Thanks @dvd, it worked perfectly! =)

-1

var texto = "assets/pdf/regulamento_Oi_Mod.pdf assets/images/oi-mod-tela-controle-2x.gif";
texto.replace(/\/((?=pdf|images)pdf|images)\//g, "\\/$1\\/")
console.log(texto);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.