如何在 Javascript 中搜索字符串的所有实例[SM_g]randomword[SM_h].
并将它们全部替换为[SM_g]randomword.[SM_h]
.
我也想用逗号来做到这一点。例如 - 我需要能够[SM_g]randomword[SM_h],
变成[SM_g]randomword,[SM_h]
.
到目前为止,这是我的代码:
const periodRegex = /\[SM_g](.*?)\[SM_h](?:[.])/g;
string_afterregex - string_initial.replace(periodRegex, /*I don't know what goes in here*/)
捕获模式,然后使用反向参考重新排序:
const periodRegex = /(\[SM_g].*?)(\[SM_h])([.,])/g;
var string_initial = '[SM_g]randomword[SM_h].'
var string_afterregex = string_initial.replace(periodRegex, "$1$3$2");
console.log(string_afterregex);
var string_initial = '[SM_g]randomword[SM_h],'
console.log(string_initial.replace(periodRegex, "$1$3$2"))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句