JavaScript Regular expression to create multiple word fragments based off the same words regular expression create online,regular expression create,regular expression create

Let's say I have the following string:

var str = "I like barbeque at dawn";

I want pairs of all words which are separated by a space. This can be achieved via the following regular expression:

  var regex = /[a-zA-Z]+ [a-zA-Z]+/g;
  str.match(regex);

This results in:

["I like", "barbeque at"]

But what if I want ALL permutations of the pairs? The regular expression fails, because it only matches any given word onces. For example, this is what I want:

["I like", "like barbeque", "barbeque at", "at dawn"]

I know I can use the recursive backtracking pattern to generate permutations. Do regular expressions have the power to create these types of pairs for me?

Answer:1

You can do the following:

(\w+)\s+(?=(\w+))

and capture the pairs with ($1, $2)

See DEMO

Input: i like barbeque at dawn

Output: (i, like) (like, barbeque) (barbeque, at) (at, dawn)
Answer:2

You can use lookaheads for this:

var str = "i like barbeque at dawn";
var regex = /(?=\b([a-zA-Z]+ [a-zA-Z]+)\b)/g;
var matches= [];

while ((match = regex.exec(str)) != null) {
    if (match.index === regex.lastIndex)
       regex.lastIndex++;
    matches.push(match[1]);
}

console.log(matches);
//=> ["i like", "like barbeque", "barbeque at", "at dawn"]
Answer:3

Use a lookahead with a capture, which allows overlapping matches:

(\w+)\s+(?=(\w+))

Demo

Alternative if you want to capture in one group vs two:

(?=(\b\w+\s+\b\w+))

Demo

Answer:4

Here's my code: var APP = {} APP.item = function() { var two = function() { return "three"; } return { two: two }; } console.log(APP.item.two); Now, from what I've read, ...

Here's my code: var APP = {} APP.item = function() { var two = function() { return "three"; } return { two: two }; } console.log(APP.item.two); Now, from what I've read, ...

  1. revealing module pattern javascript
  2. revealing module pattern design pattern
  3. revealing module pattern es6
  4. revealing module pattern vs class
  5. revealing module pattern typescript
  6. revealing module pattern jquery
  7. revealing module pattern constructor
  8. revealing module pattern
  9. revealing module pattern vs prototype
  10. revealing module pattern inheritance
  11. revealing module pattern parameters
  12. revealing module pattern disadvantages
  13. revealing module pattern prototype
  14. revealing module pattern init
  15. revealing module pattern extend
  16. revealing module pattern global variables
  17. revealing module pattern public variables
  18. revealing module pattern singleton
  19. revealing module pattern dependencies
  20. revealing module pattern namespace

Is there a way to listen for new sqlsrv data in node.js (or PHP 5.5) without having to poll the database constantly? I am (obviously, I suppose) trying to have mssql notify my application when new ...

Is there a way to listen for new sqlsrv data in node.js (or PHP 5.5) without having to poll the database constantly? I am (obviously, I suppose) trying to have mssql notify my application when new ...

I know this is a somewhat nebulous question but which algorithm would be more efficient and/or "faster"? Searching through a preloaded JavaScript array of names or using the MySQL query "SELECT NAME ...

I know this is a somewhat nebulous question but which algorithm would be more efficient and/or "faster"? Searching through a preloaded JavaScript array of names or using the MySQL query "SELECT NAME ...

  1. what more efficient ac or dc
  2. what's more efficient than led
  3. what more efficient
  4. which is more efficient in converting biomass
  5. which is more efficient recursion or iteration
  6. which is more efficient otto or diesel

I have an angular project set up that when a user goes to /cloud and they aren't logged in (isLoggedIn() return fails), it redirect to /login. And then if they are logged in and go to /login it ...

I have an angular project set up that when a user goes to /cloud and they aren't logged in (isLoggedIn() return fails), it redirect to /login. And then if they are logged in and go to /login it ...