简体   繁体   English

Jquery从textarea获取随机单词

[英]Jquery get random words from textarea

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). 我有一个带有一些文本的textarea,我想从文本中获取5个随机单词并将它们放在另一个输入字段中(自动)。 I dont want to be specific words. 我不想成为具体的话。 Random 5 words. 随机5个字。 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 commodo 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." “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 commodo consequat.Duis aute irure dolor in rephenderit 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. 包含此文本写入时的输入字段,请说:ipsum,amet,veniam,velit,deserunt。

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);
}

This is my suggestion for the work flow: 这是我对工作流程的建议:

  1. Get the words from the textarea 从textarea获取文字
  2. Remove duplicates 删除重复项
  3. 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 jsFiddle中的工作示例

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM