Replacing space and comma from a string using regex

I may get a string like this:

23,45,567
23,45 56 7

I want it to free of all the commas and spaces. So, I tried to use the following codes:

“23,45,567”.replace(’/^[\s,]$/g’, ‘’)

“23,45,567”.replace(’/^[\s,]+$/g’, ‘’)

“23,45,567”.replace(’/^\s,$/g’, ‘’)

And many more like this. But none of it is working. Any help regarding this matter will be helpful.

Thank you

Your regex doesn’t work because first, you’ve put the regex into quotes, so you’re checking if your string contains this substring: '/^[\s,]$/g'. It’s not evaluated as a regex anymore, but as a string literal.

Also, you use ^ and $, which mean that the string must start with whatever comes after ^ and must end with whatever comes before $. If you just remove those, your regex works fine:

str.replace(/[\s,]/g, '')

Thank You @jsdisco for your quick reply and solution to my problem.

Happy to help, regex can be horribly confusing. By the way, if you really just want to remove two characters (space and comma), I think I’d just chain two lovely brand new .replaceAll() methods, because it’s easier to read (and write):
str.replaceAll(',', '').replaceAll(' ', '')