Split string with custom condition in javascript (Not Duplicate) [duplicate] - javascript

This question already has answers here:
Split a string by commas but ignore commas within double-quotes using Javascript
(17 answers)
Closed 4 years ago.
I have following string :
var str = '15156,"ABALONE, FRIED",60.1,189,19.63,,,,';
i want to split it in the following way :-
[15156, "ABALONE, FRIED", 60.1, 189, 19.63, null, null, null]
i have try this:-
var strArray = str.split(",");
output:-
[15156, "ABALONE", "FRIED", 60.1, 189, 19.63]
How can i get this using javascript's split function or any other way.

Convert that into array like string representation and you can parse it to get the desired output:
var str = '15156,"ABALONE, FRIED",60.1,189,19.63';
var res = JSON.parse('[' + str + ']')
console.log(res);

Related

How to trim string between given parameter from existing string in javascript [duplicate]

This question already has answers here:
How to remove the end of a string, starting from a given pattern?
(5 answers)
Closed last month.
I have the string "/Employee/Details/568356357938479"; and I want to obtain the new string of only "/Employee" from the given string?
var myString = "/Employee/Details/568356357938479";
var newString = myString.replace(/\\|\//g, '');
I'm expecting : "/Employee"
try this:
var myString = "/Employee/Details/568356357938479";
var newString = myString.substring(0, myString.indexOf("/", 1));
console.log(newString);

Remove string quotes from a array javascript? [duplicate]

This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Convert string into an array of arrays in javascript
(3 answers)
Closed 2 years ago.
I'm trying to remove " " from an array inside a string.
var test = "['a']"
var test1 = "['a','b']"
Expected Output:
var test_arr = ['a']
var test1_arr = ['a','b']
I tried replacing, didn't work
var test_arr = test.replace(/\"/, '');
I see two ways to accomplish that.
JSON.parse('["a","b"]') note that the values need to be in double-quotes.
"['a','b']".replace(/[['\]]/g, '').split(',') note that you need to split after replacing the unwanted chars
Both yield an array containing the original strings.
You can simply convert the single quotes inside the strings to double quotes first to convert the string to a valid JSON, and then we can use JSON.parse to get the required array like:
var test = "['a']"
var test1 = "['a','b']"
var parseStr = str => JSON.parse(str.replace(/'/g, '"'))
var test_arr = parseStr(test)
var test1_arr = parseStr(test1)
console.log(test_arr)
console.log(test1_arr)

TO convert a string to array in javascript [duplicate]

This question already has answers here:
Convert string in array format to javascript array [duplicate]
(3 answers)
Closed 3 years ago.
I have a String like
var str = "['one','two']"
i want to convert it into a array, what are the possibilities ?
is there a elegant solution than stripping quotes
You can use the replace() and split() methods:
var str = "['one','two']";
str = str.replace( /[\[\]']+/g, '' );
var strarr = str.split( ',' );
console.log( strarr )

How to parse a string, which is a list of ints into a list in Javascript? [duplicate]

This question already has answers here:
Convert JSON string to array of JSON objects in Javascript
(6 answers)
Closed 5 years ago.
I have a string which is a list of ints, and I need to parse it into a list of those ints. In other words, how to convert "[2017,7,18,9,0]" to [2017,7,18,9,0]?
more info
When i console.log(typeof [2017,7,18,9,0], [2017,7,18,9,0] ), I get: string [2017,7,18,9,0]. I need to convert it into a list such that doing console.log(), i get: object Array [ 2017, 7, 18, 9, 0 ].
Thanks in advance for any help!
You can use .match() with RegExp /\d+/, .map() with parameter Number
var res = "[2017,7,18,9,0]".match(/\d+/g).map(Number)
You can try parsing it as JSON:
console.log(JSON.parse("[2017,7,18,9,0]"))
Or you could try to manually parse the string:
var str = "[2017,7,18,9,0]";
str = str.substring(1, str.length - 1); // cut out the brackets
var list = str.split(",");
console.log(str);
var yourString = "[2017,7,18,9,0]";
var stringArray = JSON.parse(yourString);
console.log( JSON.stringify(stringArray), typeof stringArray);
Is it JSON.parse that you need?
By splitting with regex (because who doesn't love a regex):
var array = "[2017,7,18,9,0]".split(/\,|\[|\]/).shift().pop();
The shift and pop remove the empty strings from the front and back of the array, which are from the open and close brackets.
These solutions are turning into a list of every available option. So in that spirit I'll throw eval() into the mix.
eval("[2017, 23, 847, 4]");
NOTE: the other answers are much better and this method should not be used.
var strArray = "[2017, 23, 847, 4]";
var realArray = eval(strArray);
console.log(realArray);

splitting a string based on delimiter [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I split a string, breaking at a particular character?
I have a string in following format
part1/part2
/ is the delimiter
now I want to get split the string and get part 1. How can I do it?
result = "part1/part2".split('/')
result[0] = "part1"
result[1] = "part2
split the string and get part 1
'part1/part2'.split('/')[0]
var tokens = 'part1/part2'.split('/');
var delimeter = '/';
var string = 'part1/part2';
var splitted = string.split(delimeter);
alert(splitted[0]); //alert the part1
var result = YourString.split('/');
For your example result will be an array with 2 entries: "part1" and "part2"

Categories