Javascript regex matching all word pairs

javascript regex matcher
regex backreference javascript
regex capture group example
regex match group of words
regex match brackets
regex match pattern multiple times
regex match second group
regular expression examples

Why use a regex for that, a simple loop with a split would work perfectly. :

let str = "This is a string of words that I want to pair"

let words = str.split(' ');
let finalArray = []
for(var i = 0 ; i < words.length; i++ ) {
  // validate that the next words exists
  if(words[i + 1]) {
    let pairedWords = words[i] + " " + words[i + 1];


Regular expression to match word pairs joined with colons, You can use the regex: \w+:\w+. Explanation: \w - single char which is either a letter(uppercase or lowercase), digit or a _. \w+ - one or more of  The string#match with a global flag regex returns an array of matched substrings. The /\d{3}/g regex matches and consumes (= reads into the buffer and advances its index to the position right after the currently matched character ) 3 digit sequence.

You may use this regex with a lookahead:


This regex matches a single word with a lookahead condition that says that matches word must be followed by 1+ whitespaces followed by another word. We capture matched word and lookahead word in 2 separate groups that we have to concatenate in final result.

const s = 'This is a string of words that I want to pair';

const regex = /\b(\w+)(?=(\s+\w+))/g;

var result = [];
var m;

while ((m = regex.exec(s)) != null) {
  result.push(m[1] + m[2]);


Regex Tutorial, Suppose you want to match a pair of opening and closing HTML tags, and the text in between. Count the opening parentheses of all the numbered capturing groups. The word boundary \b matches at the > because it is preceded by B. The word boundary does not make the engine In JavaScript it's an octal escape. my favorite tool, it can make a diagram of how your regex will work, you can add multiple lines to test if the regex match the strings that you expect and also has a cheatsheet Similar to debuggex except it doesnt generate the diagram, but in my opinion their cheatsheet is cleaner and easier to find what you need.

I think something like that should works:

    const s = "This is a string of words that I want to pair";
    console.log([...(s.matchAll(/[^ ]+(?: [^ ]+)?/g))]);

Pattern Matching with Regular Expressions (JavaScript: The , The JavaScript RegExp class represents regular expressions, and both String and regular expression literals are specified as characters within a pair of slash As we've seen, all alphabetic characters and digits match themselves literally in Match exactly three word characters and an optional digit /\s+java\s+/ // Match  Supports JavaScript & PHP/PCRE RegEx. Results update in real-time as you type. Roll over a match or expression for details. Validate patterns with suites of Tests. Save & share expressions with others. Use Tools to explore your results. Full RegEx Reference with help & examples. Undo & Redo with {{getCtrlKey()}}-Z / Y in editors.

[PDF] Regular Expressions: The Complete Tutorial, Matching Floating Point Numbers with a Regular Expression . The fact that this “a” is in the middle of the word does not matter to the regex engine. If it matters to This regex contains only one pair of parentheses, which capture the string matched JavaScript and Python apply all mode modifiers to the entire regular. (6) As @Jared Ng and @Issun pointed out, the key to solve this kind of RegEx like "matching everything up to a certain word or substring" or "matching everything after a certain word or substring" is called "lookaround" zero-length assertions. Read more about them here. In your particular case, it can be solved by a positive look ahead.

A Practical Guide to Regular Expressions (RegEx) In JavaScript, There are two ways to create a regular expression in Javascript. It can be either This method returns an array containing all the matched groups. It accepts a A word character is any letter, digit, and underscore. (Same as  The method regexp.exec(str) method returns a match for regexp in the string str. Unlike previous methods, it’s called on a regexp, not on a string. It behaves differently depending on whether the regexp has flag g. If there’s no g, then regexp.exec(str) returns the first match exactly as str.match(regexp). This behavior doesn’t bring anything new.

Regular Expression (Regex) Tutorial, Character: All characters, except those having special meaning in regex, matches themselves. E.g., \bcat\b matches the word "cat" in the input string. In Perl (and JavaScript), a regex is delimited by a pair of forward slashes (default)​, in the  If you want to force a.m. and p.m. to be words, (they're not, they're acronyms) then add period as a word character for your regex engine. For you it appears you've set period as not a word character, so therefore regex words won't be one-to-one and onto for the standard definition of "word" that you were taught in your European Dictionary for your hybrid European language (or any other