需要一个正则表达式来在javascript中拆分字符串(Need a regular expression to split string in javascript)

我需要一些javascript正则表达式的帮助。 我有一个字符串:

var S = '["abc","defg", "hij"]';

我怎么能在javascript中拆分它得到a[0]=abc , a[1]=defg , a[2]=hij ? 因为var a = S.split(','); 给我a[0]=["abc"等等。 非常感谢你。

I need some help with regular expressions in javascript. I've got a string like:

var S = '["abc","defg", "hij"]';

How could I split it in javascript to get a[0]=abc, a[1]=defg, a[2]=hij? Because var a = S.split(','); just give me a[0]=["abc" and so on. Thank you very much.

最满意答案

如果您修复了用于分隔字符串的引号,那么您可以将字符串JSON.parse转换为数组并根据需要使用它,如下所示:

var s = '["abc","defg", "hij"]';
var arr = JSON.parse(s);

console.log(arr);
console.log(arr[0]); // = 'abc' 
  
 

If you fix the quotes you use to delimit the string, then you can JSON.parse the string to an array and work with it as you need, like this:

var s = '["abc","defg", "hij"]';
var arr = JSON.parse(s);

console.log(arr);
console.log(arr[0]); // = 'abc' 
  
 

需要一个正则表达式来在javascript中拆分字符串(Need a regular expression to split string in javascript)

我需要一些javascript正则表达式的帮助。 我有一个字符串:

var S = '["abc","defg", "hij"]';

我怎么能在javascript中拆分它得到a[0]=abc , a[1]=defg , a[2]=hij ? 因为var a = S.split(','); 给我a[0]=["abc"等等。 非常感谢你。

I need some help with regular expressions in javascript. I've got a string like:

var S = '["abc","defg", "hij"]';

How could I split it in javascript to get a[0]=abc, a[1]=defg, a[2]=hij? Because var a = S.split(','); just give me a[0]=["abc" and so on. Thank you very much.

最满意答案

如果您修复了用于分隔字符串的引号,那么您可以将字符串JSON.parse转换为数组并根据需要使用它,如下所示:

var s = '["abc","defg", "hij"]';
var arr = JSON.parse(s);

console.log(arr);
console.log(arr[0]); // = 'abc' 
  
 

If you fix the quotes you use to delimit the string, then you can JSON.parse the string to an array and work with it as you need, like this:

var s = '["abc","defg", "hij"]';
var arr = JSON.parse(s);

console.log(arr);
console.log(arr[0]); // = 'abc'