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 )
Related
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);
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)
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);
This question already has answers here:
Backslashes - Regular Expression - Javascript
(2 answers)
Closed 6 years ago.
I have an problem with constructing regex from variable.
var a = '.playlist-item:nth-child(2n+1)';
var selector = /.playlist-item:nth-child\(2n\+1\)/g;
var s = '.playlist-item:nth-child\(2n\+1\)';
console.log(selector.test(a))//true
var reg = new RegExp(s,"g");
console.log(reg.test(a) )//false
Second is false because I have string quotes around it (I think), how do I construct regexp from string?
https://jsfiddle.net/eq3eu2e8/1/
For a string you have to use double backslashes if you want to include them in the string:
var a = '.playlist-item:nth-child(2n+1)';
var selector = /.playlist-item:nth-child\(2n\+1\)/g;
var s = '.playlist-item:nth-child\\(2n\\+1\\)';
console.log(selector.test(a)); //true
var reg = new RegExp(s,"g");
console.log(reg.test(a)); //false
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"