[英]Javascript regex match different groups
I want to replace different parts of a string with a word that's inside of every matching group. 我想用每个匹配组内的单词替换字符串的不同部分。 So, if I have this string:
所以,如果我有这个字符串:
<td>{$phrase->getId()}</td>
<td>{$phrase['name']}</td>
<td>{$phrase['id']}</td>
I would like to be able to get the string: 我希望能够得到字符串:
<td>id</td>
<td>name</td>
<td>id</td>
I tried this expression: 我试过这个表达式:
\{\$\w+(?:\['(\w+)'\]|->get(\w+)\(\))\}
but when I input.replace(regex, "$1")
I get: 但是当我输入
input.replace(regex, "$1")
我得到:
<td></td>
<td>name</td>
<td>id</td>
replace with $1$2
since you have used |
用
$1$2
代替,因为您使用过|
or operator 或运算符
like this : input.replace(regex, "$1$2");
像这样:
input.replace(regex, "$1$2");
for the first sentence the Id
is coming inside the second capture group, thus it is not matching the first one. 对于第一个句子,
Id
进入第二个捕获组,因此与第一个不匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.