simov/slugify

How to replace all special characters with replacer?

mahammad-sixberries opened this issue · 3 comments

How to replace all special characters with replacer?
Trott commented

Sounds like you want a regular expression and not a slug library. (And if I'm misunderstanding your request, sorry! There's not a lot of information provided. If explaining in text is difficult, share some code that you've tried and indicate the resulting string you're trying to get and the resulting string you're getting instead.)

I'm not sure what you mean by "special characters" but since I always see that term in password requirements to mean something that isn't an English alphabet letter or number, here's what that would look like:

const string = 'MySecureP@ssw0rd!';
string.replace(/[^A-Za-z0-9]/g, '-'); // 'MySecureP-ssw0rd-'

I'm going to close this but feel free to leave a comment with more information and re-open (if the GitHub interface lets you re-open it--but you can leave a comment either way).

while using strict mode, there are some cases in which special characters are need to replace instead of removing it, like

example.com should be example-com instead of examplecom

or

test@example.com should be test-example-com

or

abc/xyz should be abc-xyz instead of abcxyz

so it would be more usefull if there is an additional option for this along with strict mode, which additionally does string.replace(/[^A-Za-z0-9]/g, replacement)

Also can be just like remove option but instead of removeing characters that match regex, it replaces characters that match regex

slugify('some string', {
  replace: /[^A-Za-z0-9]/g, // replace characters that match regex, defaults to `undefined`
})