[英]Regex to detect the first set of square brackets with anything inside them
所以我在RegExes吮吸。 仍然。
我正在使用jQuery循环一些HTML输入,并且,在他们的名称属性中,我想仅替换第一组方括号中的“任何”。
我有这些HTML输入:
<fieldset id="set">
<input type="text" name="shift[305][sub_amount]" value="Snoopy" />
<input type="text" name="shift[405][price]" value="12" />
<input type="text" name="cost[insert_1392222118][amount]" value="Hoof" />
</fieldset>
我的目标就是这个
<fieldset id="set">
<input type="text" name="shift[replaced][sub_amount]" value="Snoopy" />
<input type="text" name="shift[replaced][price]" value="12" />
<input type="text" name="cost[replaced][amount]" value="Hoof" />
</fieldset>
所以我像这样循环它们:
$('#set').find(":input").attr("name", function(index, element) {
return element.replace(/\[[0-9]+\]/, function(match) {
return '[replaced]';
})
});
但是我的正则表达式只修复了带有数字数据的方括号。
JSFiddle-example只在方括号只包含数字数据时解决它: http : //jsfiddle.net/XLnAC/
谁可以告诉我哪个RegEx用于定位方括号的第一个实例以及其中的任何内容?
我发现了类似的问题,但没有详细说明如何只查找第一个实例。 用于检测圆括号或方括号的正则表达式
你可以用这个:
/\[[^\]]+\]/
您也不需要在这里使用函数:
return element.replace(/\[[^\]]+\]/, '[replaced]')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.