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
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
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/
var a = "[{abc,1},{eee,2},{fff,5,jjj},{kkk,9}]".slice(2, -2).split('},{');
これを試してみてください
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( /^\{/, '' );
これが私があなたが望むと信じているものです。
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*{)
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.