Swap the text within the curly braces with the div element containing the specified text

I have an input and a textarea. Using Vue, I am currently setting the textarea's text to match what's in the input field. However, now I want to be able to change the color of specific text by typing something like {#123123}text{/#}. At this point, I don't necessarily need the color change functionality, but I'm wondering how to identify the text between {#} and {/#} and wrap it in a div that changes its color.

I'm unsure where to begin with this task. My initial thought is to create a function that searches for {, then identifies the text following it before locating }. Nevertheless, I am uncertain about the best approach to achieving this.

Answer №1

If you want to achieve this, regex can help you accomplish it. Here's a sample code snippet:

const input = document.querySelector('input');
const textArea = document.querySelector('textArea');

input.oninput = process;

function process() {
  const match = input.value.match(/{(#[^}]+)}([^{]+){\/#}/);
  if (match) {
    const [, color, text] = match;
    textArea.value = text;
    textArea.style.color = color;
  }
}

process();
<input type="text" value="{#a83232}text{/#}" />
</br>
</br>
<textarea></textarea>

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

Is there a way for me to access this user's data with just a click?

When selecting a user from the list, more information should be displayed on the right. However, currently, a random user is being shown. Should I retrieve its index and compare them? Assistance is needed to clarify this issue and locate the necessary info ...

The content remains constant when navigating within the same view

Vuejs3 Composition API Route File: Below is the route configuration for SingleBoard.vue, which displays content based on passed props: const routes = [ { path: "/singleboard/:dbName/:dbMethod/:securityType/:alertType", name: & ...

An issue arose in nodejs when attempting to use redirect, resulting in the error: "Error [ERR_HTTP_HEADERS_SENT]: Unable to modify headers after they have

I am currently working on a project where I encountered an unexpected error. My goal was to redirect the server to specific routes based on certain conditions, but I am facing difficulties. routes.post("/check", (req, res) => { console.log(& ...

The value of Yargs.argv is consistently displayed as [object Object]

In my Ubuntu 16.04 environment, I enrolled in a node.js course on Udemy. Following the instructor's guidance, I initially used the exact version mentioned and later updated to the latest version (11.0.0). Surprisingly, both versions yielded the same o ...

There seems to be a lack of information appearing on the table

I decided to challenge myself by creating a simple test project to dive into Angular concepts such as CRUD functions, utilizing ngFor, and understanding HttpClient methods (specifically get and post). After creating a Firebase database with an API key and ...

How can I change the attributes of icon().abstract.children[0] in the fontawesome-svg-core api?

The issue at hand: The icon() function within the fontawesome-svg-core API is setting default properties for SVG children elements that require custom modifications. My objective: The outcome of the icon() method is an object with an "html" property, co ...

Fulfill the specified amounts for each row within a collection of items

I have an array of objects containing quantities. Each object includes a key indicating the amount to fill (amountToFill) and another key representing the already filled amount (amountFilled). The goal is to specify a quantity (amount: number = 50;) and au ...

Retrieve input value in Angular 8 using only the element's ID

Can the value of an input be obtained in Angular 8 with TypeScript if only the element's id is known? ...

Unable to convert date data for display on D3 line graph

I am attempting to replicate a line graph similar to the one in the following fiddle link: http://jsfiddle.net/wRDXt/2/ In the example, the date is used as new Date(2013, 17, 1). The JSON data I have is structured as shown below, [{ "_id": "bb68d8a2 ...

Converting RSS title to HTML format with the help of JavaScript

I am currently working on populating a menu on a webpage with the 5 latest topics from our site's RSS newsfeed using JavaScript (specifically jQuery version 1.9.1). I have been searching for a solution, but most answers I find reference deprecated scr ...

I am experiencing issues with the customsort function when trying to sort a column of

Seeking assistance with customizing the sorting function for a Date column in a primeng table. Currently, the column is displaying data formatted as 'hh:mm a' and not sorting correctly (e.g. sorting as 1am, 1pm, 10am, 10pm instead of in chronolog ...

Utilize React and Jest to handle errors by either mocking window values or resolving them

When my app attempts to inject environmental variables at runtime for docker using the window object, I encounter errors in my tests. The code snippet below shows the configuration: url config: declare const window: Window & typeof globalThis & ...

Is there a way to implement multiple "read more" and "read less" buttons on a single page?

I am currently working on a rather large project and I am encountering some issues with the read more buttons. As someone who is fairly new to JavaScript, I am still grappling with the concepts. While I have managed to make the function work for the first ...

The data is not retrieved in the response

When creating an order, I am encountering an issue where the code is not waiting for the meal data retrieval despite using async and await. This results in my response containing empty data. Although I prefer to use async and await over promises for consi ...

Keeping Vuex state in harmony with Firebase

When it comes to keeping my Vue.js state in sync with Firebase, I have implemented a strategy that involves setting up references to Firebase, getters, mutations, and actions in a separate folder within the store. So far, everything seems to be working fin ...

A self-destructing javascript/jquery function that removes itself after running

For a very detailed analytics insight, I am sending an ajax request to my controller in order to update the user's interaction (such as which pages they visit or like) and store this information in my MongoDB. All I want is for this script to be dele ...

Using an external font in JavaFX CSS

Can you style a font with CSS in a JavaFX application? I have an FXML scene and corresponding CSS file. In Java code, you can set a font using the setFont method: text.setFont(Font.loadFont("file:resources/fonts/SourceSansPro-Bold.ttf", 12)); I've t ...

Spring application encountering 404 error following nginx request

My Vue application is connected to two different backend services. One of them uses SpringBoot, which is currently not compatible with nginx. The configuration in my nginx.conf.template file includes: location /apcc { proxy_pass ${APCC_BACKEND_URL}; ...

Adjust the color of error messages in shiny from red

Currently, I am in the process of developing a Shiny app that includes numerous plots. Whenever I adjust any input parameters, there is a brief moment where the plots do not display before they are rendered, accompanied by a prominently displayed red error ...

Personalize the build folder within the 'npm start' command

I've developed an Angular app using npm and web-pack, which is then served on a Tomcat web server. When the command npm run build is executed, all JS files are built into the /build directory of the project. However, when running npm start, it uses t ...