Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取javascript中除最后一次出现以外的所有模式_Javascript_Regex - Fatal编程技术网

获取javascript中除最后一次出现以外的所有模式

获取javascript中除最后一次出现以外的所有模式,javascript,regex,Javascript,Regex,鉴于以下模式: "profile[foreclosure_defenses_attributes][0][some_text]" "something[something_else_attributes][0][hello_attributes][0][other_stuff]" 我能够使用非捕获组提取最后一部分: var regex = /(?:\w+(\[\w+\]\[\d+\])+)(\[\w+\])/; str = "profile[foreclosure_defenses_attri

鉴于以下模式:

"profile[foreclosure_defenses_attributes][0][some_text]"
"something[something_else_attributes][0][hello_attributes][0][other_stuff]"
我能够使用非捕获组提取最后一部分:

var regex = /(?:\w+(\[\w+\]\[\d+\])+)(\[\w+\])/;
str = "profile[foreclosure_defenses_attributes][0][properties_attributes][0][other_stuff]";
match = regex.exec(str);
["profile[foreclosure_defenses_attributes][0][properties_attributes][0][other_stuff]", "[properties_attributes][0]", "[other_stuff]"]
然而,我希望能够得到除最后一部分以外的一切。换句话说,除了[一些文本]或[其他东西]以外的一切

我不知道如何对非捕获组执行此操作。我还可以如何实现这一点呢?

类似的东西

 shorter, and matches from the back if you can have more of the [] items.

var regex = /(.*)(?:\[\w+\])$/;
var a = "something[something_else_attributes][0][hello_attributes][0][other_stuff11][other_stuff22][other_stuff33][other_stuff44]".match(regex)[1];
    a; 
或者使用替换,尽管性能较差

 var regex = /(.*)(?:\[\w+\])$/;

 var a = "something[something_else_attributes][0][hello_attributes][0][other_stuff11][other_stuff22][other_stuff33][other_stuff44]".replace(regex, function(_,$1){ return $1});
     a; 

如果这些确实是您的字符串:

var regex = /(.*)\[/;

是的,这对我有用。出于好奇,你为什么使用替换?我发现exec更简洁,我更新了一些简短的东西。当我在命令行中进行匹配时,匹配的使用是任意的,只是更容易键入。请删除误用的替换示例好吗?它将导致不必要的性能损失,而匹配方法同样简洁。确定。根据请求更新。