There are multiple solutions on StackOverflow to repace multiple characters in a string and from that I have this code:
var replaceChars = { ' ':'-', 'ń':'n', 'ó':'o', 'ż':'z', 'ą':'a' };
var answerStrUrl = answerStr.replace(/ń|ó|ż|ą/g,function(match) {return replaceChars[match];}).toLowerCase();
(answerStrUrl is defined earlier in the code)
This works for all the characters but what should I add in the second line so that spaces are replaced with ‘-‘
2
Answers
Add the
|s+|
in the replace function.Build the regexp dynamically and use
[]
character group: