i have example string:
var string = 'this süper nice sentence, right?'; the result has be:
this, is, süper, nice, sentence requirements:
- 5 words max,
- words contain @ least 2 characters
- comma separated
- takes care of special characters such ü this not happening
- all in lowercase this not happening
this current script: (you can test in jsfiddle)
var string = 'this süper nice sentence, 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 best way convert matched words lowercase before join?
the answer tolowercase(), think best place run right @ end rather beginning (fewer items operate on):
if(words != null) { //5 words maximum words = words.slice(0,5); if(words.length) { console.log(words.join(', ').tolowercase()); //here } } tolowercase() is, far know, unicode-friendly. regex stripping not a-z,a-z.
asker found link helpful resolving regex issue: regular expression match non-english characters?
Comments
Post a Comment