Home > Blockchain >  Remove all matching characters from the beginning of word
Remove all matching characters from the beginning of word

Time:11-15

I have this regex:

string.replace(/[~!@#$%^&`*()_\ ={}[\]|"':;?,\/><,\\]|- $|- (?=\.)|\s|^[.]/g, '')

and I have this string: .....domain@#@#$-.com

I want to return domain.com. It works almost fine but it removes only first dot at the beginning, but I want to remove any of dot if it appeal here.

I added something like that ^[.], but I think that I miss $ or sign. But any of my approach doesn't work. Any ideas?

CodePudding user response:

You have to add the * to ^[.] at the end of your pattern, [.] this means match only one literal dot. * means match zero or more literal dot from the start of the string . so it will be ^[.]*.

[~!@#$%^&`*()_\ ={}[\]|"':;?,\/><,\\]|- $|- (?=\.)|\s|^[.]*

See regex demo

  • Related