Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Regex - Whitespace, comma and slash

Would like to replace all instances of whitespace, commas and slashes in a string with a hyphen.

At the moment I am using

myString..replace(/\s+/g, "-").replace(/\//g, "-").replace(/,/,"-");

which is not nice I know but I tried something along the lines of

myString.replace(/s+,\//g, "-");

but to no avail.

How should I organise the regex?

like image 217
user2085143 Avatar asked Jan 08 '23 00:01

user2085143


1 Answers

Put all inside a char class except \s+.

myString.replace(/\s+|[,\/]/g, "-");
like image 78
Avinash Raj Avatar answered Jan 09 '23 18:01

Avinash Raj