[英]get index value from string representing array jquery
given string is 给定的字符串是
var strg = "RoomItemsQuantity[items][1][itemId]"
I'm having the above string value.How can i get itemId from the string. 我有上面的字符串值。如何从字符串中获取itemId。
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, 谢谢,
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. 会给你你想要的。
If the above pattern is fixed, then you could use the following trick : 如果上述模式是固定的,则可以使用以下技巧:
var myString = "RoomItemsQuantity[items][1][itemId]";
var myItemId = (myString+"[").split("][")[2]
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. 您也可以使用向前或向后搜索,但可能无法获得支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.