Reputation: 18103
I got this:
var stringToReplace = 'æøasdasd\89-asdasd sse';
var desired = stringToReplace.replace(/[^\w\s]/gi, '');
alert(desired);
I found the replace rule from another SO question.
This works fine, it gives output:
asdasd89asdasd sse
Although I would like to set up additional rules:
æøå
characters-
character-
characterSo the output would be:
æøåasdasd89-asdasd-sse
I know I can run an extra line: stringtoReplace.replace(' ', '-');
to accomplish my 3) goal - but I dont know what to do with the 1 and 2), since I am not into regex expressions ?
Upvotes: 1
Views: 1960
Reputation: 785058
This should work:
str = str.replace(/[^æøå\w -]+/g, '').replace(/ +/g, '-');
Upvotes: 2
Reputation: 8265
You can just add the special characters to the exclusion list.
/[^\w\sæøå-]/gi
Fiddle with example here.
And as you said - you can use another replace to replace spaces with dashes
Upvotes: 1
Reputation: 11610
Your original regex [^\w\s]
targets any character which isn't a word or whitespace character (-
, for example). To include other characters in this regex's 'whitelist', simply add them to that character group:
stringToReplace.replace(/[^\w\sæøå-]/gi, '');
As for replacing spaces with hyphens, you cannot do both in a single regex. You can, however, use a string replacement afterwards to solve that.
stringToReplace.replace(" ","-");
Upvotes: 0