【发布时间】:2014-08-23 16:09:51
【问题描述】:
我正在为产品变体构建一个“添加新行”功能,并且我正在努力使用匹配表单属性键所需的正则表达式。所以,我基本上是在克隆行,然后增加键,就像这样(coffeescript):
newrow = oldrow.find('select, input, textarea').each ->
this.name = this.name.replace(/\[(\d+)\]/, (str, p1) ->
"[" + (parseInt(p1, 10) + 1) + "]"
)
this.id = this.id.replace(/\_(\d+)\_/, (str, p1) ->
"_" + (parseInt(p1, 10) + 1) + "_"
)
.end()
这会正确递增名称为 product[variations][1][name] 的字段,将其转换为 product[variations][2][name]
但是每个变体都可以有多个选项(例如,颜色可以是红色、蓝色、绿色),所以我需要能够将这个product[variations][1][options][2][name] 转换为product[variations][1][options][3][name],而不用单独保留变体键。我需要什么正则表达式来匹配最后一次出现的键(选项键)?
【问题讨论】:
标签: javascript regex coffeescript