Javascript separate string by line pushing into array [duplicate] - javascript

This question already has answers here:
How to split newline
(13 answers)
Closed 3 years ago.
I have a string with multiple lines.
one
two
three
I need to add them to an array separated by line
How can I do that?

You can use string.split to cut your string at newlines. You can add a Array.filter to remove the empty lines.
Filter will loop at every string and create a new array. If the string is empty it will not push it to the new array.
const str = `one
two
three`;
const ret = str.split('\n').filter(x => x.length);
console.log(str);
console.log(ret);

You can use String.prototype.split() such as below
const someString = `one
two
three`;
const myArray = someString.split('\n');
console.log(myArray);

Related

How to split a string into a specific number of words and put it together as a single sentence in JavaScript [duplicate]

This question already has answers here:
Shorten string without cutting words in JavaScript
(27 answers)
Closed 1 year ago.
I need to split a big string into 5 words and return a sentence from these 5 words. Example:
// The string that needs to be split
'Hello, this is a very big string that goes on for loooooooooong'
// The output i need
'Hello, this is a very
I know I can use split(' ', 5) to separate the first 5 words, but i don't know how to put them back together into a sentence. Thanks in advance for your help and i you need me to clearer just ask.
You just need to rejoin the splitted array:
split(' ', 5).join(' ');
Using Array.prototype.join() which creates and returns a new string by concatenating all of the elements in an array.
const s = 'Hello, this is a very big string that goes on for loooooooooong';
const str = s.split(' ', 5).join(' ');
console.log(str);
More about Array.prototype.join() - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
You can easily achieve the result using split, slice and join and using regex /\s+/
const str = "Hello, this is a very big string that goes on for loooooooooong";
const result = str.split(/\s+/).slice(0, 5).join(" ");
console.log(result);

How to get entire array as string? [duplicate]

This question already has an answer here:
array join() method without a separator
(1 answer)
Closed 3 years ago.
I need to get entire array result as a clear string like: namegenderage, but can't figure out how
I tried:
var arr = ["name","gender","age"];
var string = arr.toString().replace(",","");
console.log(string);
log result should be string like namegenderage without quotes and brackets but I get this result "namegender,age"
You can just use the join() method:
var arr = ["name","gender","age"];
var string = arr.join('');
console.log(string);

regex lazy repeat [duplicate]

This question already has answers here:
Why this javascript regex doesn't work?
(1 answer)
How to match multiple occurrences of a substring
(3 answers)
Closed 4 years ago.
I am trying to replace 「.file extension,」 into 「,」
「1805171004310.jpg,1805171004311.png,1805171004312.jpg,」 into 「1805171004310,1805171004311,1805171004312,」
How can I make it lazy and repeat?
https://jsfiddle.net/jj9tvmku/
dataArr = new Array();
dataArr[1] = '1805171004310.jpg,1805171004311.png,1805171004312.jpg,';
fileNameWithoutExt = dataArr[1].replace('/(\.(.*?),)/', ',');
$('#msg').val(fileNameWithoutExt);
https://regex101.com/r/nftHNy/3
Just use the global flag g.
Your regex, isn't actually a regex, it's a string. Remove the single quotes surrounding it: /(\.(.*?),)/g, And you can remove all the capture groups, since are not needed here: /\..*?,/g
const dataArr = new Array();
dataArr[1] = '1805171004310.jpg,1805171004311.png,1805171004312.jpg,';
const fileNameWithoutExt = dataArr[1].replace(/\..*?,/g, ',');
console.log(fileNameWithoutExt);
// or an array of filenames
console.log(fileNameWithoutExt.split(',').filter(Boolean));
If you want the file names individually, use .split(',').filter(Boolean)

Spliting numbers out of a string contain characters and numbers [duplicate]

This question already has answers here:
Extract numbers from a string using javascript
(4 answers)
Closed 6 years ago.
I want to split the numbers out of a string and put them in an array using Regex.
For example, I have a string
23a43b3843c9293k234nm5g%>and using regex I need to get [23,43,3843,9293,234,5]
in an array
how can i achieve this?
Use String.prototype.match()
The match() method retrieves the matches when matching a string against a regular expression
Edit: As suggested by Tushar, Use Array.prototype.map and argument as Number to cast it as Number.
Try this:
var exp = /[0-9]+/g;
var input = "23a43b3843c9293k234nm5g%>";
var op = input.match(exp).map(Number);
console.log(op);
var text = "23a43b3843c9293k234nm5g%>";
var regex = /(\d+)/g;
alert(text.match(regex));
You get a match object with all of your numbers.
The script above correctly alerts 23,43,3843,9293,234,5.
see Fiddle http://jsfiddle.net/5WJ9v/307/

How to split comma separated string using JavaScript? [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 3 years ago.
I want to split a comma separated string with JavaScript. How?
var partsOfStr = str.split(',');
split()
var array = string.split(',')
and good morning, too, since I have to type 30 chars ...
var result;
result = "1,2,3".split(",");
console.log(result);
More info on W3Schools describing the String Split function.
Use
YourCommaSeparatedString.split(',');

Categories