Replacing space and comma from a string using regex

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, '')