1

I have a string like [{abc,1},{eee,2},{fff,5,jjj},{kkk,9}]. I want to split this string using flower braces.Output must be,

abc,1

eee,2

fff,5,jjj

kkk,9

I need to find out the no of elements. For above example no of elements is 4

4

5 に答える 5

1

Craft a regular expression to remove the [,],{ and } characters.

var str = "[{abc,1},{eee,2},{fff,5},{kkk,9}]";
str = str.replace(/[\[\]{}]/g, "");

Working Example: http://jsfiddle.net/72Kdd/

于 2013-03-02T14:57:06.490 に答える
1
var a = "[{abc,1},{eee,2},{fff,5,jjj},{kkk,9}]".slice(2, -2).split('},{');
于 2013-03-02T15:01:06.197 に答える
0

これを試してみてください

var x = '[{abc,1},{eee,2},{fff,5,jjj},{kkk,9}]';
var items = x.match( /\{[^{]*(?=\})/g );
for ( var i in items ) items[ i ] = items[ i ].replace( /^\{/, '' );
于 2013-03-02T15:25:41.627 に答える
0

これが私があなたが望むと信じているものです。

var str = "[{abc,1},{eee,2},{fff,5},{kkk,9}]";

Array.prototype.map.call( // for each of
    str // the string's
        .match(/\{(.*?)\}(?:,(?=\s*{)|]$)/g), // matches to a certain pattern
    function (e) {
        return e.slice(1,-2); // take the middle chars
    }
);
// Array ["abc,1", "eee,2", "fff,5", "kkk,9"]

注:これは、カンマをチェックした後の楽しみのために、"a{bc,1}"から{a{bc,1}}、および "a{bc},1"からを与えます。{a{bc},1}(?=\s*{)

于 2013-03-02T15:09:50.407 に答える
0

You could try replacing out the braces. Something like:

String.replace("{").replace("}").split(",");

Alternatively, turn it into a json object and iterate over its members.

于 2013-03-02T14:55:34.613 に答える