Home > Back-end >  JavaScript Regex for removing certain char
JavaScript Regex for removing certain char

Time:06-25

I would like to filter this string

Field Strength                 3T  

and make it

Field Strength: 3T  

What javascript regex should I use? I need to remove the extra spaces, add :, and add 1 space

CodePudding user response:

Try this code:

const regex = /\s{2,}/g;
const str = "Field Strength                 3T  ";
console.log(str.trim().replace(regex, ": "));

  • Related