Reputation: 11
I need to come up with some regex or javascript that will as the title says get 1 random word from every X consecutive words.
for e.g I have this piece of text
"In computing, a regular expression provides a concise and flexible means to "match" (specify and recognize) strings of text, such as particular characters, words, or patterns of characters. Common abbreviations for "regular expression" include regex and regexp."
I would like to regex 1 word every 4 words so from the first four words in the text "In computing, a regular" I would regex out 1 random word e.g computing
The need to do this for the whole text however so for example in the above there are 9 groups of 4 words. I would like to regex out one random word from each group. Hopefully I can make a quick edit in that regex so it can do the same for X word groups as well. X being any number.
I have tried all over to get something that can do this for me but I'm not getting on very well (I am new to both regex and javascript)
I think if I can use javascript to split the text into X groups of words, I can then try to get a random word from each group afterwards.
What I have so far is this:
var split='In computing, a regular expression provides a concise and flexible means to "match" (specify and recognize) strings of text, such as particular characters, words, or patterns of characters. Common abbreviations for "regular expression" include regex and regexp.'.match(/[^ ]+( +[^ ]+){0,3}/g);
This splits the text with a comma separator into groups of 4 words (or every 3 spaces). The problem is that the separator is a comma so any comma's in the original text are also seen in the output e.g
In computing, a regular,expression provides a concise,and flexible means to,"match" (specify and recognize),strings of text, such,as particular characters, words,,or patterns of characters.,Common abbreviations for "regular,expression" include regex and,regexp.
Is there anyway to change that separator to something besides a comma? At least this way I can then try and come up with some regex that can be used on each 4 word group. I thought of maybe replacing the comma after the match but that will replace all the original commas as well of course.
I'm not sure how I will do the random word regex or javascript but having the groups sorted out I think is the first step in the right direction?
Thanks for any help or advice with this, I really appreciate it. Sorry if this is completely straightforward to some of you but I am new do this and I've tried for hours now to come up with some working solution but to no avail.
Peter
Upvotes: 0
Views: 431
Reputation: 23796
var str = "In computing, a regular expression provides a concise and flexible means to \"match\" (specify and recognize) strings of text, such as particular characters, words, or patterns of characters. Common abbreviations for \"regular expression\" include regex and regexp.";
var words = str.match(/\w+/g);
for (var i=0; i< words.length; i+=4) {
var randNum = Math.floor(Math.random()*4) + i;
if (words[randNum]){
document.write(words[randNum] + "<br>");
}
}
Edit:
If you want to ensure that the last "group" of words always has a value, then you can do this:
var words = str.match(/\w+/g);
for (var i=0; i< words.length; i+=4) {
var maxRand = Math.min(4, words.length - i);
var randNum = Math.floor(Math.random()*maxRand) + i;
document.write(words[randNum] + "<br>");
}
So, if you have the phrase "one two three four five six" the first word will be a random word from the first four words, the second group only contains the words "five" and "six" so you'll get a random word from one of those TWO words.
Upvotes: 1
Reputation: 33544
.match
will return an array of matches. Then just split them by space and get a random one out of each array:
var text = '"In computing, a regular expression provides a concise and flexible means to "match" (specify and recognize) strings of text, such as particular characters, words, or patterns of characters. Common abbreviations for "regular expression" include regex and regexp."';
var groups = text.match( /[^ ]+( +[^ ]+){0,3}/g );
for( var i=0 ; i<groups.length ; i++ ) {
var words = groups[i].split( ' ' );
console.log( words[~~(Math.random()*words.length)] );
}
-
~~(Math.random()*words.length)
will give you an integer 0
to # of words (4) - 1.
Upvotes: 1
Reputation: 69673
Regular expressions don't support randomization, so they won't help you here.
But you can use the String.split method to transform the text into an array of individual words. Then you iterate the array in in groups of four and select a random word from each group.
Math.floor(Math.random() * 4)
returns a random integer between 0 and 3.
Upvotes: 1