How to convert comma separated strings enclosed within bracket to array in Javascript?

Stack Overflow Asked by Arif on October 16, 2020

How to convert below string to array in Javascript? The reason is that I want to take both value separately.
The string is value from an element, when I print it to console I got:(‘UYHN7687YTF09IIK762220G6′,’Second’)

var data = elm.value;

2 Answers

You can achieve this with regex, like this for example :

const string = "('UYHN7687YTF09IIK762220G6','Second')";
const regex = /'(.*?)'/ig

// Long way
const array = [];
let match;
while (match = regex.exec(string)){

// Fast way
console.log([...string.matchAll(regex)].map(i => i[1]))


Correct answer by Namysh on October 16, 2020

let given_string = "('UYHN7687YTF09IIK762220G6','Second')";

// first remove the both ()
given_string = given_string.substring(1); // remove (
given_string = given_string.substring(0, given_string.length - 1); // remove )

let expected_array = given_string.split(',');

Answered by sazzad on October 16, 2020

Add your own answers!

Related Questions

Truncated backpropagation in PyTorch (code check)

1  Asked on December 7, 2021 by rampatowl


Blurred places in TextField

1  Asked on December 7, 2021


Masonry style menu columns in mega-menu

2  Asked on December 7, 2021 by joe-rr


How to call useState from another Page?

3  Asked on December 7, 2021 by grizzly-bear


Get Base64 encode file-data from Input Form

7  Asked on December 7, 2021 by jordan-baucke


Restart TeamCity server via web interface

2  Asked on December 7, 2021 by krlmlr


SliverOverlapAbsorber being able to accept multiple slivers

1  Asked on December 7, 2021 by basic-coder


Pandas plot – Range

1  Asked on December 7, 2021 by user13770492


Alternate row colours from 2nd row of the table using CSS

5  Asked on December 7, 2021 by raj-kumar-boddu


MsSql Grammar quoteString don’t auto add ‘N’

0  Asked on December 7, 2021 by jay-chu


Why loading a library dynamically makes my app crash?

0  Asked on December 7, 2021 by jpo38


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir