javascript - Regex to capture consecutive words -


i have list of mobile device names like

"sony xperia z1 compact" "samsung galaxy trend plus" "samsung galaxy tab 2" 

i need regex create tags strings match consecutive words in way (expected result):

["sony", "sony xperia", "sony xperia z1", "sony xperia z1 compact"] 

i have tried positive lookahead:

/(?=([a-z]+\s+[a-z]+))[a-z]+/i 

i obtained:

model = "samsung galaxy trend plus" "samsung galaxy trend plus" model.match(/(?=([a-z]+\s+[a-z]+))[a-z]+/i) ["samsung", "samsung galaxy"] 

but not work when have 1 word, so, after adding optional group:

/(?=([a-z]+\s+[a-z]+))|[a-z]+/i 

i obtained:

model = "samsung" "samsung" model.match(/(?=([a-z]+\s+[a-z]+))|[a-z]+/i) ["samsung", undefined] 

so, trying generalize:

/(?=([a-z]+\s+[a-z]+))(?=([a-z]+\s+[a-z]+\s+[a-z]+))(?=([a-z]+\s+[a-z]+\s+[a-z]+\s+[a-z]+))[a-z]+/i 

i get

"samsung galaxy trend plus" model.match(/(?=([a-z]+\s+[a-z]+))(?=([a-z]+\s+[a-z]+\s+[a-z]+))(?=([a-z]+\s+[a-z]+\s+[a-z]+\s+[a-z]+))[a-z]+/i)  ["samsung", "samsung galaxy", "samsung galaxy trend", "samsung galaxy trend plus"] 

and supposed have n words, how make regex generic (a degree of freedom on n words variable)? also, how rid of undefined ? explained here should use non-capturing group, prevent me capture consecutive words in way doing.

i can suggest pure regex solution. because need swap word order in order correct output subphrases:

var re = /(?=\b((?:\s+[ \t]*)+))/g;   var str = 'sony xperia z1 compact';     str = str.split(' ').reverse().join(' ');  while ((m = re.exec(str)) !== null) {      if (m.index === re.lastindex) {          re.lastindex++;      }      document.getelementbyid("t").innerhtml += m[1].split(' ').reverse().join(' ') + "<br/>";  }
<div id="t"/>

the (?=\b((?:\s+[ \t]*)+)) regex capturing chunks of non-whitespace characters followed optional whitespace (but not newline symbol) , make sure whole words using \b word boundary.

i think difficult without reversing since not have variable width lookbehind in js.


Comments