Is there a way to divide all the characters within a string, excluding the sequence " "?

My issue (resolved)


I've been working on an animation where each letter appears sequentially, but I encountered a problem. In order to transform the letters properly, I had to wrap my span tags in a flex div because using inline-block didn't allow for the desired transformation. However, this caused the spaces between words to disappear, resulting in the words being concatenated without any space.

I'm utilizing NuxtJS for this task. Here is the current code snippet:

<div :class="$style['intro-chars']">
  <span
   v-for="(char, i) of text.split('')"
   :key="i"
   ref="title"
   :class="{ space: i == 6 }"
  >
  {{ char }} <!-- "text" is defined in a data() method -->
  </span>
</div>
.intro-chars {
    display: flex;
    overflow: hidden;
    span {
      font-size: 6em;
      font-weight: 700;
    }
  }

.space {
  opacity: 0;
}
let title = this.$refs.title;
console.log(title);

for (let i = 0; i < title.length; i++) {
  gsap.timeline({ defaults: { duration: 1.8, ease: "power4.inOut", delay: 0.1 * i } }).fromTo(title[i], { translateY: 100 }, { translateY: 0 });
}

Solution


I found a solution by processing my data in a computed method.

To address the whitespace issue, I replaced all spaces in my string with underscores (_).

I then manipulated the data by replacing the underscore with \u00A0 using the following logic:

computed: {
 splitText() {
   let split = this.text.split('');
   split[split.indexOf("_")] = "\u00A0"
   return split;
}

In the v-for loop, instead of (char, i) of text, it now reads (char, i) of splitText (as done in the computed method).

This approach displays &nbsp; within a single span element, rather than breaking it into multiple separate span elements like

<span>&</span> <span>n</span> <span>b</span>…
.

Answer №1

From my understanding, you are looking to separate a string into individual characters without including any spaces.

If that's the case, you can use the following code snippet:

message.replace(/ +/g, '').split('');

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

Open multiple accordion sections simultaneously

In my current setup, I have a paginated loop of elements that contains an accordion. The accordion is implemented using angular-ui-bootstrap. <div data-ng-repeat="m in results"> <div class="stuff_in_the_middle"> <accordion id="a ...

Issue encountered: ENOENT error - The specified file or directory does not exist. This error occurred while attempting to access a directory using the

I don't have much experience with nodejs or express, but I have an API running on http://localhost:3000. One of the endpoints triggers a function that uses the file system to read a file synchronously. However, when I send a post request using Postman ...

Changing the color of an open Bootstrap 4 accordion panel when the page loads

I am looking to emphasize the panel that the user has opened. If a user clicks the button in the card header, the background turns red, which is working smoothly as demonstrated in the code snippet. In certain scenarios, the first panel is open by default ...

Sharing API types from a NestJs backend to a Vue frontend (or any other frontend) application: Best practices

In my development setup, I have a VueJs and Typescript Front End app along with a PHP server that is in the process of being converted to NestJs. Both of these projects reside in the same Monorepo, allowing me to share types for DTOs between them. I am ex ...

Invoking a Vuex action inside another Vuex action

After transitioning to the Vuex store, I've been steadily moving forward. However, my current hurdle involves invoking an action from within another action in Vuex. My grasp on Vue.js is still a work in progress. Current versions Vue 3 Vuex 4 If nec ...

Utilizing Ajax to dynamically display the outcome of selecting a radio button

I am currently working on a script that aims to utilize Ajax in order to post the result of a radio button click. My progress so far includes: <label> <input type="radio" name="DisableAd" value="0" id="RadioGroup1_0" /> Radio </lab ...

Incorporate different courses tailored to the specific job role

https://i.stack.imgur.com/iGwxB.jpg I am interested in dynamically applying different classes to elements based on their position within a list. To elaborate: I have a list containing six elements, and the third element in this list is assigned the class ...

The deployed app on Heroku is showing a 304 status code with no JSON response

I've been working on a project using React, Express, and MongoDB. While everything works fine locally, I encountered an issue with GET requests after deploying the project on heroku.com. Instead of receiving the expected JSON response, I'm gett ...

What is the best way to create a list from a matrix using JavaScript?

I have an array structured as follows: const input_array= [ ["red", "green"], ["small", "medium"], ["x", "y", "z"] //... can have any number of rows added dynamically ...

What is the most foolproof method for detecting when a checkbox has been marked as checked, regardless of the circumstances?

Is there a way to detect changes in the checked value of a checkbox when it is changed by a script that I do not control? I have tried using `addEventListener()` and jQuery's `on()` method, but they do not work in all cases. <input type="checkbox" ...

Update database upon drag-and-drop using jQuery

I have a dataset (shown below) that undergoes sorting by 'section'. Each item is placed into a UL based on its section when the page loads. My goal is to automatically update the section in the database when a user drags an item to a different s ...

Searching for variables within files using Node.js and constructing an object from the results

Trying to figure out how to streamline this process. Here's the directory structure I'm working with: src/ modules/ clients/ i18n/ en-US.ts tasks/ i18n/ en-US.ts So, ea ...

Discover the steps to inserting an icon into a tab using AngularJS

Hello, I am a beginner in angular js. Can someone please guide me on how to include an icon in a tab using angular js? Here is my current code: <tabset panel-tabs="true" panel-class="{{demoTabbedPanelClass}}" heading="{{demoTabbedPanelHeading}}"> ...

Ways to separate a string based on changing values in Javascript

Given this unmodifiable string: "AAACCDEEB" I am looking to split it into an array whenever the value changes. In this scenario, I would end up with 5 arrays like so: [['A','A','A'], ['C','C'], [ ...

Exploring the limitations of middlewares in supporting independent routers

When I examine the code provided, it consists of three distinct routers: const Express = require("express") const app = Express() // Three independent routers defined below const usersRouter = Express.Router() const productsRouter = Express.Router() cons ...

How to clear input fields in Ant Design ReactJS components

Utilizing the form list component from antd in my react.js project https://ant.design/components/form/#Form.List I have a basic form list set up where I can easily add or remove fields. At times, I need to reset the form and remove any additional items t ...

Error: The code is trying to access the property 'string' of an undefined variable. To fix this issue, make sure to

I encountered an issue after installing the https://github.com/yuanyan/boron library. The error message I received is: TypeError: Cannot read property 'string' of undefined Error details: push../node_modules/boron/modalFactory.js.module.expor ...

Execute ReactJS function only if query parameters are configured

Provide an Explanation: Within the useEffect, I am retrieving products using the getProducts() function based on the provided data. The data contains search filters that can be updated by the user in real-time. For instance, the data consists of an object ...

One way to generate div elements based on the number in an input field when a button is clicked, but ensuring it only happens once

What I am attempting to achieve is: Retrieve data from a JSON file upon button click. Display the data in separate boxes, each one different for every element of the array. For instance, if the JSON provides 3 rows of data, there should be 3 distinct box ...

What is the best way to link a mutable object with an immutable reactive object?

A unique composable function called useApplicationPreferences() is utilized in Vue 2 with the Composition API. This specific function provides access to a non-writable computed ref named darkMode: //useApplicationPreferences.ts import { reactive, computed ...