This question already has answers here:
Split string at space after certain number of characters in Javascript
(5 answers)
Split large string in n-size chunks in JavaScript
(23 answers)
Closed 5 months ago.
MESSAGE FOR MOD: This question is about keeping full sentences, the linked questions only consider words.
Take the following example:
// I have a long string that I split into sentences
const input =
"This is the first sentence... This is the second, much longer sentence, with some additional puntuations?! Third sentence with a different length! Just a sentence ending with a number 980. Last but not least, the fourth sentence.";
// I used the following code to split the long string into sentences:
const arr = input.replace(/([.?!])\s*(?=[a-zA-Z0-9])/g, "$1|").split("|");
// we can assume that a sentence from the input array does not exceed this limit
const maxLength = 105;
// TODO: magic happens
/* I'm trying to get an array with the sentences re-joined
by a space, split so that one string does not exceed the limit
[
"This is the first sentence... This is the second, much longer sentence, with some additional puntuations?!",
"Third sentence with a different length! Just a sentence ending with a number 980.",
"Last but not least, the fourth sentence."
]*/
codesandbox: https://codesandbox.io/s/string-array-split-max-chunk-length-bfteuh?file=/index.ts
Im trying to get an array that combines the strings of the initial array with a space but considers that the resulting strings cannot exceed a maximum length. Also the strings have to contain full sentences (you can assume a single sentence won't exceed the limit). Also consider "...", "?!", "???", etc. as possibile sentence endings in the original input string.
How would you go about this? Do I have to use recursion to get some kind of concise code? Is recursion the most elegant solution?
Note: So far I tried a reducer but thought that I would have to use the rest of the array in a recursive function.
Related
This question already has answers here:
how to extract floating numbers from strings in javascript
(3 answers)
Closed 2 years ago.
Lately iv'e been trying to find some ways to manipulate a string (for some project of mine) and i'm having a hard finding something that will mach my case.
usually the string will include 3 numbers (can also be decimal - that's what make it more complicated) and separated by 1 / 2 signs ("-", "x", "*" and so on...)
i did some research online and found this solution (which i thought it was good)
.match(/\d+/g)
when i tried it on some case the result was good
var word = "9-6x3"
word = word.match(/\d+/g)
it gave me array with 3 indexes, each index held a number ['9', '6', '3'] (which is good), but if the string had a dot (decimal number) this regex would have ignored it.
i need some regex which can ignore the dots in a string but can achieve the same result.
case =
var word = "9.5-9.3x7" output = ['9.5', '9.3', '7']
Try this regular expression to allow for an optional decimal place:
word.match(/\d+([\.]\d+)?/g)
This says:
\d+ - any number of digits
([\.]\d+)? - optionally one decimal point followed by digits
Here is a simple regex that suits your requirement,
/\d+\.?\d*/g
I need to split a payload in Node-RED whenever it is longer than a certain number of characters, and after a certain number of decimals.
I am working on a project where a sensor is providing feedback to Node-RED, but it sometimes puts two outputs together, and I can't seem to find a way to split the resulting data into two parts at a position which is not at the decimal point, but a number of digits AFTER the decimal point.
At the moment, I am scrapping the wrong outputs using
if (msg.payload.length < 11){return msg;}
so that only single output results are processed further, while anything else is discarded.
Output can be like 123.4567123.4687 instead of 123.4567and 123.4687.
Note that the problem only occurs sometimes (something like every 100th measurement).
Note that the number of digits BEFORE the decimal point is not necessarily the same every time, so it is not just a matter of splitting after a certain number of digits from the first.
If the number of digits after the decimal point is constant you could use a regular expression to extract the needed values, for example:
var input = "123.4567123.4678";
var results = input.match(/\d+\.\d{4}/g);
results is an array that contains the two values as strings: [ '123.4567', '123.4678' ]
The Regex globally matches one or more digits (\d+) followed by a point (\.) followed by four digits (\d{4})
I am parsing a string of multiple numbers between 1 and 10 with the eventual goal of adding them to a set.
There will be multiple concatenated numbers after a text identifier such as {text}12345678910.
I am currently using match(/\d/g) to grab the numbers but it separates 1 and 0 in 10. I then look for 0 in my String Array, see if there's a 1 in the element before it, turn it into a 10 and delete the other entry. Not very elegant.
How can I clean up my matching code? I definitely don't need to use regex for this, but it makes grabbing the numbers fairly easy.
You could just match with this regex:
/10|\d/g
(instead of the one you use currently, not additionally)
Regex is executed left-to-right, so first it finds any occurrences of 10, and then of other digits (so using, for example /\d|10/g or even /\d|(10)/g won't work either).
This question already has answers here:
How to match multiple occurrences of a substring
(3 answers)
Closed 2 years ago.
Let's say I have an input field and want to parse all of the numbers from the submitted string. For example, it could be:
Hi I'm 12 years old.
How do I parse all of the numbers without having a common pattern to work with?
I tried:
x.match(/\d+/)
but it only grabs the 12 and won't go past the next space, which is problematic if the user inputs more numbers with spaces in-between them.
Add the g flag to return all matches in an array:
var matches = x.match(/\d+/g)
However, this may not catch numbers with seperators, like 1,000 or 0.123
You may want to update your regex to:
x.match(/[0-9 , \.]+/g)
var words = sentence.split(" ");
var numbers = words.filter(function(w) {
return w.match(/\d+/);
})
i have a word from A to Z. all word should in small latter (Capital not include) and 1 to 9 (included all special word who can be used in email address (just for a test)).
how i can generate unique 1 lacs text who never repeat itself. can anyone solve this puzzle.
i want a another thing that all words should not more then 10 char and not should minimum 6 char long
Put the characters in an array. Copy the array as the source of a new line. Randomly slice words from the array and put them in the line (use Math.random() * array.length | 0). Keep going for the required number of words.
You can also just use a string and charAt(index) if you only want single characters, but you have to keep cutting out the character that you select which is likely less efficient than using array.slice.
Whatever suits though, since performance is likely irrelevant.