简体   繁体   English

如何从字符串中删除最后一个'[]'?

[英]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.

相关问题 如何从字符串中删除最后一个逗号并将其替换为Javascript中的句点? - How do I remove the last comma from my string and replace it with a period in Javascript? 如何从字符串中删除第一个和最后一个字符? - How can I remove the first and last characters from a string? 如何从字符串中删除数组? - How do I remove an array from a string? 如何在点击时从输入字段中删除最后一个字符? - How do I remove last character from input field on click? 如何从While循环中的最后一个字符串中删除最后一个字符? - How to remove the last character from last string in a While loop? 如何使用正则表达式删除字符串中最后一个字母字符后的任何非字母字符? - How do I use regex to remove any non alpha characters after last alpha character in string? 我如何使用 JS 从嵌套字符串中删除第三个和最后一个双引号(如果不可能,我如何使用正则表达式来完成) - How can i remove the 3rd and last double quote from a nested string using JS (if not possible, how can i do it using Regex) 如何从字符串中删除列表短语? - How do I remove a list phrases from a string? 如何从Javascript字符串中删除特定的“序列”? - How do I remove a specific “sequence” from a Javascript string? 如何从字符串中删除前100个单词? - How do I remove the first 100 words from a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM