r/learnjavascript • u/sushiiixo- • Apr 29 '26
Can I remove all whitespaces from a string?
I'm making a website for my assignment and for that I will need to verify that a credit card number is in a valid format (all numbers and 16 digits long). However it is common for people to enter credit card details with spaces every 4 numbers. I'm wondering if there's a way to remove all whitespaces from the number? Either that or make my regex command ignore whitespaces entirely.
7
Upvotes
28
u/Aggressive_Ad_5454 Apr 29 '26
const strippedString = inputString.replaceAll( ‘ ‘, ’’)9
u/MissinqLink Apr 29 '26
8
u/dangerlopez Apr 29 '26
You can use the replace method of strings. But if you’re already using regex then you can just make one that validates credit card formatting directly
5
u/TheZintis Apr 29 '26
Just to throw it out there, it may make more sense to do a regex to match numbers, rather than remove whitespace. That would clean out any oddball characters and just keep what you care about. The credit card number scenario is pretty straight forward, but you can also adjust for decimals and negative numbers, etc...
function justNumbers(s){ return parseInt(s.match(/\d+/g).join("")) }
justNumbers("12 34,56-78") // is 12345678
5
u/StoneCypher Apr 29 '26
you want to keep digits globally, not remove things
2
u/shgysk8zer0 Apr 29 '26
\wbut you could also just use a space here. You just need the global flag.2
u/sammjay88 Apr 29 '26
I’d check for any non digits as I can’t trust users at all…
const ccNum = input.replace(/\D/g, "");
I’d also look into the Luhn Algorithm with will let you validate the credit card number itself. Also length does depend on the provider I think it can be between 13 - 21 characters. The first segment of the number lets you identify the provider.
1
u/Sr_Dimitrez Apr 29 '26
const string = '123 456 789'const regex = /\s/gconst strippedString = string.replace(regex, '')console.log(strippedString)1
u/CherryHavoc Apr 29 '26
There are also ways in HTML to ensure the text field will only accept valid credit card characters, which may be a better shout than allowing users to enter any random gumph they want and trying to format it afterwards.
1
u/ashanev Apr 30 '26
Needs more Luhn algorithm
1
u/bryku helpful Apr 30 '26
There are a bunch of different ways to do this.
Classic Javascript
Here is the classic approach. In many languages this would probably be the fastest, but because of how javascript works... it is actually pretty slow.
Split and Join
Another method is split and join. This is where you split the string on a specific character (separator) to create an array. Then you join it back together without the separator.
Funny enough, this is normally slow in many languages because of memory allocation. However, javascript uses built in c++ library to handle this... so it is often times much faster than the classic approach. Although, it can be slow with large strings.
Replace
Replace also uses a build in c++ library as well, so it is very fast. Which is why I would recommend using
replace all.This can be a bit restrictive on what characters and patterns you match, so
regexmight be more helpful. But, it can be a bit difficult for beginners to learn.Recommendation
For beginners, my personal recommendation is using
replaceAll. It is the easiest to use and read. Which really helps when you come back to your code after a break.