Divide the string into an array by splitting it on white space located before the 10th

I am looking for a way to break a string into chunks of 10 characters without splitting words.

For example, the string "Nine characters to go - then some more" should be split into

["Nine", "characters", "to go -", "then some", "more"]
.
Words longer than 10 characters can be split.

I tried using regex with .{1,10}(?<=\s).
However, the results were not as expected. It would sometimes skip characters or split at weird places like after a whitespace.

Another attempt involved using .split(' ') and then using various techniques like .reduce() or loops to achieve the desired result.

for ( i = 0; i < splitArray.length; i++ ) {
  if ( i === 0 ) {
    // first word in new array (no consideration for word length)
    newArray.push( splitArray[i] );
  } else {
    if ( newArray[ newArray.length - 1 ].length + splitArray[i].length + 1 < 10 ) {
      // if next word fits in current array item (considering space), add it
      newArray[ newArray.length - 1 ] = newArray[ newArray.length - 1 ] + " " + splitArray[i];
    } else if ( newArray[ newArray.length - 1 ].length + splitArray[i].length + 1 >= 10 ) {
      // next word doesn't fit
      // split word and add it to new array
      const index = 9 - newArray[ newArray.length - 1 ].length
      const prev = splitArray[i].slice( 0, index );
      const next = splitArray[i].slice( index, splitArray[i].length );
      newArray[ newArray.length - 1 ] = newArray[ newArray.length - 1 ] + " " + prev;
      newArray.push( next );
    } else {
      // add new item to newArray
      newArray.push( splitArray[i] );
    }
  }
}

Results in:

["Nine chara", "cters to g", "o - then s", "ome more"]
.
Without the else if:
["Nine", "characters", "to go -", "then some", "more"]

Another example without the else if:
["Paul van", "den Dool", "-", "Alphabet", "- word"]

However, the word "Alphabet" does not join with the hyphen, which is a limitation.

I am stuck and seeking assistance from the community to solve this issue.

Context
I need to break a user-entered string into multiple lines for display on a canvas within a limited space and with a minimum font size. This involves splitting the string into an array, iterating over it, and positioning the text accordingly.

Answer №1

const phrase = "Nine words in total - then some more"
let words = phrase.split(" ");
for(let index = 1; index < words.length; index++) {
  if(words[index].length >= 10 || words[index].length + words[index-1].length >= 10) {
     continue;
  }
  if(words[index].length < 10 && words[index].length + words[index-1].length <= 10) {
    words[index] = words[index - 1] + " " + words[index];
    words[index-1] = false;
  }

}
words = words.filter(word => word)

console.log(words);

Answer №2

Utilize the following code snippet:

console.log(
  "Only seven characters left - then more will appear"
     .match(/.{1,8}(?=\s|$)/g)
     .map(x => x.trim())
);

By using .match(/.{1,8}(?=\s|$)/g), the elements will be limited to 1 to 8 characters, and (?=\s|$) will ensure a space character or end of line is included.

Answer №3

If you find yourself in need of division, remember to utilize the .split() method:

const phrase = 'Nine words to go - and then a few more',
      
      solution = phrase.split(/(.{1,10})\s/).filter(Boolean)
      
console.log(solution)

Answer №4

I have a solution for this problem that involves a basic for loop:

const str = "Nine characters to go - then some more";

// create an array with each word
const arr = str.trim().split(' ');

// Set the desired length of the words
const length = 10;
const res = [];

/**
 * Add the first word to the result array
 * because we need to consider all words
 * greater or less than the specified `length`.
 */
res.push(arr[0]);

// Current index of the result array
let index = 0;

for (let i = 1, l = arr.length; i < l; i++) {
  /**
   * If the length of the concatenation of the
   * last word in the result array
   * and the next word is less than or equal to the length,
   * then combine them and add them to the result array.
   */
  if ((res[index] + arr[i]).length <= length) {
    res[index] += ' ' + arr[i];
  } else {
    /**
     * If not, add the current word
     * to the result array and increment the index.
     */
    res.push(arr[i]);
    index++;
  }
}

console.log(res);
.as-console-wrapper{min-height: 100%!important; top: 0}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Opening a Bootstrap Modal in React without relying on npm react-bootstrap

I've been trying to create a Modal in React.js using Bootstrap5, but I'm unable to use npm react-bootstrap for various reasons. I attempted an approach where I utilized state to set Modal classes with a button, which worked well with my NavBar, b ...

Using a series of identical divs to dynamically update the image URL

Greetings! I am a newcomer to the world of web development and I have decided to hone my skills by creating a small website for my mother! My goal is to replicate a specific div multiple times while changing only the image URL and the heading caption. < ...

JavaScript Application - Problem with Universal Windows Script

I recently built a website utilizing material design lite for the aesthetics: Here are the scripts included: <script src="./mdl/material.min.js"></script> <script src="Scripts/angular.min.js"></script> The necessary .css fi ...

Iterate through an array of objects using underscores, make alterations to specific objects, and eliminate other objects

Let's say I have an array of objects: [{"month":"03-2016","isLate":"N","transactionCount":4,"transactionAmount":8746455},{"month":"05-2016","isLate":"N","transactionCount":5,"transactionAmount":-40004952945.61},{"month":"06-2016","isLate":"N","transa ...

Check if a rotated rectangle lies within the circular boundary of the canvas

I have a rectangular shape that has been rotated using the ctx.rotate method, and there is also an arc on the canvas. My goal is to determine if any part of the rectangle lies within the boundaries of the arc. See the example below: https://i.sstatic.net/ ...

Tips for displaying the html content saved in the database onto an ejs webpage

This task seems simple, but I'm struggling to solve it. In my Node.js/Express webapp, I have the Quill.js editor installed. It stores my description in MySQL DB like this: <p><strong>This is the quill data. How are we doing dev?</stron ...

Ajax request missing Github Basic OAuth token in authentication process

My personal access token is not being passed to the request when I make an ajax call. I keep receiving an error message saying API rate limit exceeded for 94.143.188.0. (But here's the good news: Authenticated requests get a higher rate limit.. I atte ...

Moving a DIV below a fixed-positioned element

I have a website with a scrollable div. It works well, but I also need an absolutely positioned div on top of it - and it still needs to scroll smoothly without any hindrance. You can check out a basic JSFiddle demonstration here: http://jsfiddle.net/41ra ...

Tips for adjusting the alignment of the Vuetify component "VDatePicker" based on the position of its parent component on the screen

Currently, I am utilizing the VMenu component from Vuetify which contains another Vuetify component called VDatePicker. The issue arises when clicking on a text field triggers the appearance of the calendar (VDatePicker). Normally, the VDatePicker componen ...

Encountering issues with asp.net code following saving a PNG file to disk - troubleshooting with iTextSharp and PDFStam

I've managed to successfully draw on an HTML5 canvas and save the canvas to a PNG file on the server. I then embed the PNG into a PDF using iTextsharp. Everything is working well, but there's one issue. My code currently displays the PNG on the ...

Creating a responsive HTML5 webpage that adjusts seamlessly to all screen sizes without the need for scrolling

I have developed an HTML5 application for mobile devices. Currently, I have managed to address width-height issues by using a simple scroll feature when necessary. However, I have encountered a challenge with a particular page that contains a lot of conten ...

When you encounter an open response and need to resend it, simply click the "Send Again

After the discontinuation of Firebug, I find myself in need of two crucial functionalities that I used frequently. To replace these features, I am wondering if there are similar options available within the default Firefox Web Console. Previously, when ma ...

Can the lexical scope of a function in JS be maintained while overriding it?

When faced with the task of modifying a function provided by a third-party module, I considered simply copying the function and making changes to it. However, the issue lies in the fact that this function relies on other functions and variables within its ...

What is the best way to simultaneously utilize two APIs where one is using HTTP and the other is using HTTPS?

What is the best way to simultaneously use two APIs, one being http and the other https, in Angular or JavaScript? ...

ReactJS attempting to invoke a class function using a dynamically generated button

When attempting to access the deletePost(index) method from the ShowPost class using a dynamically rendered button within the render() step in React, I encounter an issue. The button labeled "click me" successfully retrieves and prints the first item in my ...

Refresh gif without having to reload it in Internet Explorer 11

I'm attempting to create a feature where a gif restarts when clicked by the user, without needing to reload it (due to the heavy size of the gif which is preloaded for my application). The current code functions flawlessly on Chrome and other "modern ...

Determine in Node.js if a word has been altered or has an additional letter compared to the previous word

I'm currently developing a feature for my Discord bot that allows users to play a word game. The objective is to input words that either change one letter from the previous word or add a letter. To implement this, I am utilizing the following function ...

Ways to permit https://* within a content security policy (CSP) configuration

I'm currently incorporating CSP into my website but encountering an issue with the img-src header. I'm using NodeJS and Express to develop the site for my Discord Bot, and I want to revamp it but I've hit a roadblock. ====== This is the co ...

Extract the content inside an HTML <a> tag with a specified class and auto-populate it into a different text area

I found an HTML tag that is being generated by a WordPress plugin and it contains a random link. My goal is to automatically retrieve this generated link and place it in a textarea within a contact form. The generated code with the link (cannot be modifie ...

Creating a cascade of falling balls with a single click: Here's how!

I'm currently working on a project where I have a ball dropping from the cursor location and redropping when the cursor moves to another position. However, I want to be able to create a new ball every time I click the mouse. I attempted the following ...