Reputation: 153
given string is
var strg = "RoomItemsQuantity[items][1][itemId]"
I'm having the above string value.How can i get itemId from the string.
var index = strg.lastIndexOf("[");
var fnstrg = strg.substr(index+1);
var fnstrg = fnstrg.replace("]","");
I've done like this is there any easiest way to do this?
thanks,
Upvotes: 0
Views: 87
Reputation: 147453
You can use a regular expression like:
('RoomItemsQuantity[items][1][itemId]'.match(/([^\[]+)\]$/) || [])[1] // itemId
You can also use lookahead or lookbehind but support may not be available.
Upvotes: 1
Reputation: 5367
If the above pattern is fixed, then you could use the following trick :
var myString = "RoomItemsQuantity[items][1][itemId]";
var myItemId = (myString+"[").split("][")[2]
Upvotes: 1
Reputation: 21465
This code...
var arr = "RoomItemsQuantity[items][1][itemId]".replace(/]/g, "").split("[")
Will result in an array like this:
["RoomItemsQuantity", "items", "1", "itemId"]
Then...
arr[arr.length - 1]
Will give you what you want.
Upvotes: 2