GirkovArpa
GirkovArpa

Reputation: 4912

How to match every space preceded by at least 6 characters since the last matched space?

I'm copying this question from a recently deleted one that I thought was interesting, but was stumped trying to answer.

tl;dr:

How to match spaces preceded by at least 6 characters which do not include any matched spaces?


I am trying to write this regex to match spaces that have 6 or more characters/non-matched space between them, so that I can use .split() to break them into different lines.

Sample:

What I have so far: /(?<=.{6,})\s/g

This doesn't work correctly. For example, it matches all the spaces in the last name (split result: ['Donald', 'Ben', 'Ed', 'Jax']). Instead I want the split result to be ['Donald', 'Ben Ed', 'Jax']. How do you make it so that after the first match (the space after Donald), it starts searching from that index instead?

screenshot of regex and matches

Upvotes: 0

Views: 136

Answers (4)

Cary Swoveland
Cary Swoveland

Reputation: 110725

I have taken the following approach to avoid the use of lookbehinds as I understand they are not supported by all browsers.

If you match the following regex there is a space of interest immediately following each match.

/(?:^| ).{6}[^ \n]*(?= )/

Start your engine!

If, for example, the string were:

"Now is the time for exceptional  Rubiests to be extra vigilent in the testing phases"

there would be 8 matches (note there are two spaces before 'Rubiests'):

"Now is"
" the time"
" for exceptional"
"  Rubiests"
" to be extra
" vigilent"
" in the"
" testing"

The first character of each match but the first is a matched space; that is, that space is preceded by at least 6 characters that follow the last matched space or the beginning of the string.

These 8 matches, when concatenated, form the first part of the string. It is therefore easy to compute the index of each matched space.

Upvotes: 0

Dominique Fortin
Dominique Fortin

Reputation: 2238

The answer is (?<=(\S|(?<!\S{6,})\s){6,})\s

Test it here at Regex101

Upvotes: 1

StackSlave
StackSlave

Reputation: 10617

I would do something more like:

function separateName(name){
  const a = name.split(/\s+/);
  if(a.length > 3){
    a[1] += ' '+a.splice(2, 1);
  }
  return a;
}
const testArray = ['Kaiya Devine Rahman', 'Zunairah Field Cairns', 'Oliwia Ramos Smith', 'Donald Ben Ed Jax'];
for(let n of testArray){
  console.log(separateName(n));
}

Upvotes: 0

olkivan
olkivan

Reputation: 99

Isn't simple /(^\w+)\s(.+)\s(\w+)$/ what you really need? https://regex101.com/r/phAKGH/1

Upvotes: 0

Related Questions