I need to -automatically- generate tags for a text string. In this case, I'll use this string:
var text = 'This text talks about loyalty in the Royal Family with Príncipe Charles';
My current implementation, generates the tags for words that are 6+ characters long, and it works fine.
words = (text).replace(/[^a-zA-Z\s]/g,function(str){return '';});
words = words.match(/\w{6,}/g);
console.log(words);
This will return:
["loyalty","Family","Prince","Charles"]
The problem is that sometimes, a tag should be a specific set of words. I need the result to be:
["loyalty","Royal Family","Príncipe Charles"]
That means, that the replace/match code should test for:
I'm obviously having trouble in the second requirement. Any ideas? Thanks!
var text = 'This text talks about loyalty in the Royal Family with Prince Charles. Stop at The UK Guardian in London';
text.match(/(([A-Z]\w*\s*){2,})|(\w{6,})/g)
will return
["loyalty", "Royal Family ", "Prince Charles", "The UK Guardian ", "London"]
To fulfill the second requirement, it's better to run another regexp over the matches found:
var text = 'This is a Short Set Of Words about the Royal Family'
matches = text.match(/(([A-Z]\w*\s*){2,})|(\w{6,})/g)
matches.filter(function(m) {
return m.match(/\w{6,}/)
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With