I have this example string:
var string = 'This is a süPer NICE Sentence, am I right?';
The result has to be:
this, is, süper, nice, sentence
Requirements:
- 5 words max,
- words that contain at least 2 characters
- comma separated
- takes care of special characters such as ü this is not currently happening
- all in lowercase this is not currently happening
This is my current script: (you can test it in jsfiddle)
var string = 'This is a süPer NICE Sentence, am I right?';
var words;
words = string.replace(/[^a-zA-Z\s]/g,function(str){return '';});
words = words.match(/\w{2,}/g);
if(words != null) {
//5 words maximum
words = words.slice(0,5);
if(words.length) {
console.log(words.join(', ')); //should print: this, is, süper, nice, sentence
}
}
What would be the best way to convert the matched words into lowercase before the join
?