How to replace with regex?

Viewed 37
const data = 'I///m John'
console.log(data.(/\//gm.replace("'"))

I want I'm John. I have also to check /// is available or not. If available then change /// to ' ....

2 Answers

You can achieve this using + quantifier for one or more than one search.

replace method is available on String.prototype

const data = "I///m John";

const result = data.replace(/\/+/g, "'");
console.log(result);

I'm pretty sure you are using the .replace() in the wrong location. Try it like this.

let message = "Hello there";
message = message.replace(/(Hello)/g, "Hey");
console.log(message) // "Hey there"
Related