Javascript replace / with -
xxxxxxxxxx
// String.prototype.replace()
/* The replace() method returns a new string with one, some, or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence will be replaced. The original string is left unchanged. */
const p = 'core/photo';
p.replace('/', '-')
xxxxxxxxxx
const p = 'dog dog cat rat';
const regex = /dog/gi;
console.log(p.replace(regex, 'cow'));
//if pattern is regular expression all matches will be replaced
//output: "cow cow cat rat"
console.log(p.replace('dog', 'cow'));
// If pattern is a string, only the first occurrence will be replaced.
//output: "cow dog cat rat"
xxxxxxxxxx
var str = "Please locate where 'locate' occurs!";
str.replace("locate", "W3Schools"); //replace only replace first match from string
str.replace(/LOCATE/i, "W3Schools"); // i makes it case insensitive
str.replace(/LOCATE/g, "W3Schools"); // g replace all matches from string rather than replacing only first
xxxxxxxxxx
var string = "Javascript is fun";
var newString = string.replace("fun", "Awesome");
console.log(newString); // Javascript is Awesome
xxxxxxxxxx
you need to be very carefull to use 'MyStringggg'.replaceAll('rep','toRep')
for those coming from c# or java or ...
because replace just replcaes the first occurance but if you want replace all occurances then you need to use replaceAll
xxxxxxxxxx
//option 1
let str = 'JS will, JS will rock you!';
let newStr = str.replace('JS','JavaScript');
console.log(newStr);
//option 2
let str = 'JS will, JS will rock you!';
let newStr = str.replace(/JS/g,'JavaScript');
console.log(newStr);
xxxxxxxxxx
let str = 'JS will, JS will rock you!';
let newStr = str.replace('JS','JavaScript');
console.log(newStr);
Code language: JavaScript (javascript)
xxxxxxxxxx
var entrada = "Aprenda Javascript. JavaSCRIPT e Typescript";
var resultado = entrada.replace(/script/gi, "Script");
console.log({entrada, resultado})
/* Valor retornado:
{
entrada: 'Aprenda Javascript. JavaSCRIPT e Typescript',
resultado: 'Aprenda JavaScript. JavaScript e TypeScript'
}
*/
// Please register your like
xxxxxxxxxx
let str = "I like to eat, eat, eat apples and bananas";
let re = /apples|bananas/gi;
let newStr = str.replace(re, (match) => {
console.log({match});
return match.toUpperCase();
});
console.log(newStr);