admin管理员组文章数量:1306138
my question is very simple but i cant figure it out how to do it.
I have a textarea with some text and I want to get 5 random words from text and put them in another input field (automatic). I dont want to be specific words. Random 5 words. That's it. Thanks!
Example:
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea modo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
Input field that contains when this text is writed let's say: ipsum, amet, veniam, velit, deserunt.
my question is very simple but i cant figure it out how to do it.
I have a textarea with some text and I want to get 5 random words from text and put them in another input field (automatic). I dont want to be specific words. Random 5 words. That's it. Thanks!
Example:
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea modo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
Input field that contains when this text is writed let's say: ipsum, amet, veniam, velit, deserunt.
Share Improve this question asked Dec 15, 2012 at 14:31 Stoyan ZdravkovStoyan Zdravkov 951 silver badge6 bronze badges 2- This is pretty straight forward -> FIDDLE – adeneo Commented Dec 15, 2012 at 15:04
- Ok, it's work in jsfiddle/cwrxV but when i try to put in my site it wont work! What's the problem, please help me to figure it out. Thanks for previous posts. My HTML code is equal to this that i pasted in jsfiddle and i load jquery code from <head>, from <body>, from external js file. Result is the same... :/ – Stoyan Zdravkov Commented Dec 15, 2012 at 16:01
3 Answers
Reset to default 4This is my suggestion for the work flow:
- Get the words from the textarea
- Remove duplicates
- Iterate the array get the word and remove it from the array (avoid duplicates)
example code:
var text = "Lorem ipsum ......";
var words = $.unique(text.match(/\w+/mg));
var random = [];
for(var i=0; i<5; i++) {
var rn = Math.floor(Math.random() * words.length);
random.push( words[rn]);
words.splice(rn, 1);
}
alert( random ):
working example in jsFiddle
This should work:
var content = $("#myTextarea").val(),
words = content.split(" ");
var randWords = [],
lt = words.length;
for (var i = 0; i < 5; i++)
randWords.push(words[Math.floor(Math.random() * lt)]);
$("#otherField").val(randWords.join(" "));
EDIT: To prevent duplicates, you can use the following:
var nextWord;
for (var i = 0; i < 5; i++)
{
nextWord = words[Math.floor(Math.random() * lt)];
if (("|" + randWords.join("|") + "|").indexOf("|" + nextWord + "|") != -1)
{
i--;
continue;
}
randWords.push(nextWord);
}
Even shorter:
var str = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt.';
function rWords( t ) {
for ( var i = 5, s = t.match( /(\d|\w|')+/g ), r = [] ; i-- ; r.push( s[ Math.random() * s.length | 0 ] ) );
return r.join( ', ' ).toLowerCase();
}
console.log( rWords( str ) );
> lorem, eiusmod, elit, dolor, do
本文标签: javascriptJquery get random words from textareaStack Overflow
版权声明:本文标题:javascript - Jquery get random words from textarea - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741791212a2397667.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论