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'], ['D'],['E','E'], ['B']]
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'], ['D'],['E','E'], ['B']]
you might want to give this a shot
"AAACCDEEB".match(/(.)\1*/g)
You'll get results like ["AAA", "CC", "D", "EE", "B"]
This code uses a regular expression pattern to match any character repeated 0 or more times, making it quite versatile.
If you're interested in learning more about regular expressions, there are plenty of resources available - they can be very handy in situations like these!
If my solution works for you, feel free to accept it as the answer :)
Perhaps a different approach would look something like this:
var str = "AAACCDEEB";
var strArray = [[], [], [], [], []];
for(var x = 0; x <= (str.length-1); x++){
var val1 = 'A';
var val2 = 'C';
var val3 = 'D';
var val4 = 'E'
var val5 = 'B';
for(var n = 0; str[x] == val1; n++){
strArray[0][n] = str[x];
}
for(var m = 0; str[x] == val2; m++){
strArray[1][m] = str[x];
}
for(var o = 0; str[x] == val3; o++){
strArray[2][o] = str[x];
}
for(var z = 0; str[x] == val4; z++){
strArray[3][z] = str[x];
}
for(var i = 0; str[x] == val5; i++){
strArray[4][i] = str[x];
}
}
document.write(strArray);
If you find this helpful, please let me know by accepting it!!
My question is regarding changing the content of a div using the `html()` method in JavaScript. I am currently working on implementing a "cancel" button and would like to revert the content back to its original value without having to completely reset it w ...
Trying to pack a simple hello world script into an executable has led to some challenges. Both pkg and nexe seem to include the entirety of Node.js in the output file, resulting in quite larger files than necessary (around 30 MB). Although EncloseJS was fo ...
My goal is to determine the position of an x and y location when a user clicks on an image. This information will be used to add a spot to the image that displays relevant information. The content for the spot will be dynamically loaded from a SQL database ...
Implementing validation for email-based input has been a bit of a challenge for me. I have managed to set up the debounce function for onChange event handling, but encountered a strange issue. The problem arises when the user submits an invalid string bef ...
Just dipping my toes into the world of VueJS. I've got a side project in the works that hinges on fetching User's Geolocation Data right after the main component has mounted. My code snippet is as follows: var app = new Vue({ el: '#app&a ...
I am facing an issue with a markdown filter where the content of component.doc is set to update through a websocket. Despite updating the scope's component, the filtered content remains unchanged. Is there a way to dynamically refresh the v-html in t ...
I am working on a project that involves animating a paper-icon-button with spin and opacity effects when hovering over an image using the on-mouseenter and on-mouseleave events. Everything works smoothly when hovering over the image, but I am facing an is ...
Currently, I am utilizing a JavaScript code that connects to the web3 package on Ethereum's JSON RPC API. This code is designed to iterate through each transaction in an incoming block. If the transaction involves an internal wallet, it sends the rele ...
Upon encountering this informative question, the idea of creating a jQuery compiler crossed my mind. The concept was to design a tool that could translate jQuery code into raw JavaScript code for execution. In my imagination, the process of executing a bl ...
How can I make a page redirection based on the selected option value from a bootstrap select? Here is the HTML code: <select class="selectpicker"> <option value="https://example.com/">Home</option> <option value="https://exam ...
In my project, I have implemented a pop-up dialog box that rises up from the left bottom corner as the user scrolls down the page. You can view it live at this link- However, I am facing an issue where the initial lift up of the dialog box is not smooth, ...
Why is it copying two lines when I only want one line to be cloned on button click? Is there a way to make sure that only a single line is copied each time the button is clicked? Here is my code: $(function() { $('.add-more-room-btn').clic ...
I'm working on displaying a PDF file from an external URL. My goal is to extract text from the PDF for analysis, and then present a specific section of a page to users in my React application. This will involve cropping the content using coordinates o ...
I need to display data from a JSON file in select options, and here is the structure of my JSON file: [{ "vehicle": "car1", "type": [ {"name" : "BMW", "details" : [{ "color" : "red", "price" : "50000" }, ...
One of the challenges I'm facing is detecting inappropriate language in usernames. Currently, I am using regex to validate the username based on a specific pattern: "/^[A-Za-z0-9]*(\d*\.\d*)*[A-Za-z0-9]+$/" This regex pattern allows u ...
I am currently working on a Yii2 application. My goal is to utilize the JavaScript and CSS files from the common folder in my backend. The paths for these files are within the common/web/js and common/web/css directories respectively. To achieve this, I ...
I'm currently working on a JavaScript function that takes in two strings. The first string is any sentence provided by the user, and the second string consists of letters to be removed from the original sentence. My approach involves converting both s ...
I am looking to create a navigation menu that can be easily reused across multiple pages on my bootstrap/html website. Instead of duplicating code, I want to implement a solution for creating a reusable navigation menu in bootstrap/html. How can this be ...
I'm currently working on a script that, when executed, will retrieve an instance of a Google Maps object from the webpage using JQuery. For example, if there is a map present on the page, it will be structured like this: <div class="map">....& ...
Whenever my video starts playing, the audio from my device (such as iPod or Spotify) stops. If I try to play the audio manually while the video is playing, the video freezes. Interestingly, when I tested playing an audio file directly within the app, it wo ...