I used other questions but so far only stumped.
This doesn't remove underscore.
Where is the mistake
var myString = str.toLowerCase().replace(/\W+/
myString= myString.replace('/\_/g','');
I used other questions but so far only stumped.
This doesn't remove underscore.
Where is the mistake
var myString = str.toLowerCase().replace(/\W+/
myString= myString.replace('/\_/g','');
In general, you can remove all underscores from a string using
const myString = '_123__abc___-Some-Text_';
console.log(myString.replace(/_/g, ''));
console.log(myString.replaceAll('_', ''));
However, in this question, it makes sense to combine /_/g
and /\W+/g
regexps into one /[\W_]+/g
. The \W
pattern matches any char other than ASCII letters, digits and _
, thus we need a custom character class to match both patterns.
Use
var myString = str.toLowerCase().replace(/[\W_]+/g,'');
where [\W_]+
matches one or more chars other than word chars and a _
.
See the online regex demo.