javascript - Regexp to match words two by two (or n by n) -


i'm looking regexp able match words n n. let's n := 2, yield:

lorem ipsum dolor sit amet, consectetur adipiscing elit

lorem ipsum, ipsum dolor, dolor sit, sit amet (notice comma here), consectetur adipiscing, adipiscing elit.

i have tried using \b word boundaries no avail. lost trying find regex capable of giving me n words... /\b(\w+)\b(\w+)\b/i can't cut it, , tried multiple combinations.

regular expressions not need here, other split input words. problem problem involves matching overlapping substrings, regexp not at, javascript flavor. instead, break input words, , quick piece of javascript generate "n-grams" (which correct term n-word groups).

const input = "lorem ipsum dolor sit amet, consectetur adipiscing elit";    // array of words, generate n-grams.  function ngrams(words, n) {    const results = [];      (let = 0; < words.length - n + 1; i++)       results.push(words.slice(i, + n));      return results;  }    console.log(ngrams(input.match(/\w+./g), 2));


Comments

Popular posts from this blog

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

laravel - Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: F:\project\resources\views\admin\carousels\index.blade.php) -