[英]How Do I Remove last '[]' from a string?
I have some auto-generated text that includes substrings inside '[]'. 我有一些自动生成的文本,其中包含'[]'中的子字符串。
For example: 例如:
data[0]['x'][1]['y']
I want to get rid of the last sub-string. 我想摆脱最后一个子串。
Output: 输出:
data[0]['x'][1]
Any ideas? 有任何想法吗?
var result = input.replace( /\[[^\]]*\]$/, '' )
会做的伎俩
If it's auto-generated it sounds like it will always have the same form. 如果它是自动生成的,它听起来总会有相同的形式。 In that case, you could simply cut the string at the last occurrence of "["
在这种情况下,你可以简单地剪切最后一次出现的字符串“[”
var str = "data[0]['x'][1]['y']";
str = str.substring(0, str.lastIndexOf("["));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.