Reputation: 4912
I'm copying this question from a recently deleted one that I thought was interesting, but was stumped trying to answer.
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?
Upvotes: 0
Views: 136
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]*(?= )/
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
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
Reputation: 99
Isn't simple /(^\w+)\s(.+)\s(\w+)$/
what you really need?
https://regex101.com/r/phAKGH/1
Upvotes: 0