Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove space between words in string javascript js React [closed]

I'm using React (hooks) and in a component I have postcodes with a space in them in strings: eg "B72 1JL".

I need to remove the space in the middle of the postcodes so it is "B721JL" and also possibly any spaces before or after the postcode.

I've googled for ages and cannot find anything that will work. I know i probably need regex...but pretty confused!
Help appreciated.

like image 640
helen8297 Avatar asked Aug 11 '26 21:08

helen8297


1 Answers

Use String.prototype.replace() to change the space for an empty space.

const str = "B72 1JL";

// Replacing " " (space) to "" empty space
const res = str.replace(/ /g, '')
console.log(res); // BJ721JL
like image 83
Claudio Busatto Avatar answered Aug 14 '26 11:08

Claudio Busatto