Azizi Musa
Azizi Musa

Reputation: 1029

regex split string at specific length and ignore incomplete word

I want to split text when length is 30 including space. My work so far:

var m = "Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co";

var spacedM = m.split(' ');
var charCount = 0;

for(var i = 0; i < spacedM.length; i++){

    charCount = charCount + spacedM[i].length + 0.5; 

if(charCount <= 30 && $('#address1').text().length <= 30){
    $('#address1').append(spacedM[i]+' ');
} else if(charCount > 30 && charCount <= 60 && $('#address2').text().length <= 30) {
    $('#address2').append(spacedM[i]+' ');
} else if(charCount > 60 && charCount <= 90 && $('#address3').text().length <= 30) {
        $('#address3').append(spacedM[i]+' ');
}

}

$('#address1').append($('#address1').text().length);
$('#address2').append($('#address2').text().length);
$('#address3').append($('#address3').text().length);

//output
Lorem ipsum dolor sit amet, co 31
Lorem ipsum dolor sit amet, co 31
Lorem ipsum dolor sit amet, co 31

It look like ok. But it kind of a hack too. Isn't it?. I welcome any suggestion to improve this solution. Since this code will be used to split address for older data to map it inside 3 fields of address. Below is my jsfiddle: https://jsfiddle.net/u11p6xx4/4/

UPDATED: I do not want split words. Because word in address can't split to 2 part if they are meant for 1 word. So it is actually splitting address when chars are less than 30 but don't split word. The chars can be 28 in length and then continue in #address2

Example address : Blok 53-11-04 Apartment Flamingo, Keramat Jaya 2 Persiaran Gurney

Expected :

Blok 53-11-04 Apartment
Flamingo, Keramat Jaya 2
Persiaran Gurney

Upvotes: 0

Views: 1324

Answers (3)

majidarif
majidarif

Reputation: 19985

Why can't you just use regex? Like:

var m = "Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co";

var n = m.match(/.{31}/g);
$('#address1').append(n[0]);
$('#address2').append(n[1]);
$('#address3').append(n[2]);

$('#address1').append($('#address1').text().length);
$('#address2').append($('#address2').text().length);
$('#address3').append($('#address3').text().length);

// output
// Lorem ipsum dolor sit amet, co 31
// Lorem ipsum dolor sit amet, co 31
// Lorem ipsum dolor sit amet, co 31

But what happens if there is a 4th group matched? Do you just ignore everything from the ((31*3)+1)-th character?

Update:

Try using this regex /[^\W].{1,30}(?:\s|$)/g, you'll still need to improve it but it should get you started:

var m = "Blok 53-11-04 Apartment Flamingo, Keramat Jaya 2 Persiaran Gurney";

var n = m.match(/.{1,30}(?:\s|$)/g); // or /[^\W].{1,30}(?:\s|$)/g

$('#address1').append(n[0]);
$('#address2').append(n[1]);
$('#address3').append(n[2]);

// output
// Blok 53-11-04 Apartment
// Flamingo, Keramat Jaya 2
// Persiaran Gurney

You can expirement here: https://regex101.com/r/TIRa6L/2

If you wan't a more reliable approach try a so called "address verification api". Something like:

It should be able to parse a 1 line address and convert it into the correct multi-line format.

Upvotes: 2

Kerwin
Kerwin

Reputation: 1212

May can use regexp to match it. https://regex101.com/r/IszFAZ/1

And it can support the last word with any length.

var m = "Lorem ipsum dolor sit amet, c1 Lorem ipsum dolor sit amet, co2 Lorem ipsum dolor sit amet, coo3 Lorem ipsum dolor sit amet, c4";
console.log(m.match(/(?!\s).{30,}?(?=\s|$)/g));

Upvotes: 0

blaze_125
blaze_125

Reputation: 2317

Using a For loop like your original post. Not sure what your requirements are as far as truncation goes though. This snippet does not care about truncating words. It just splits at every 30 chars.

<!-- goal is to split text when length is 30 including space -->
var m = "Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co Lorem ipsum dolor sit amet, co";

var spacedM = m.split('');
var charCount = 0;
var theString = "";
var theStrings = [];
for(var b=0; b < spacedM.length; b++)
{
    theString = theString + spacedM[b];
    if(charCount == 29)
    {
        theStrings.push(theString);//add this string to the array of strings
        theString = "";//reset theString
        charCount = 0;//reset the charCount
    }
    charCount++;//increment the charCount
}

for(var i=0; i < theStrings.length ;i++)
{
    console.log(theStrings[i]);
}

Upvotes: 0

Related Questions