我曾在PHP工作preg_replace,但我不得不将它移动到JavaScript代码,它不起作用。我已经在这里搜查的话题,但还没有找到工作的解决方案,我......我 PHP看起来像:PHP preg_replace in javascript
preg_replace('/^\d+_/', '', $docTypes);
而现在,我的JS的样子,重要的事情,var documentTypes
是object
:
var documentTypesObj = {};
var counter = 0;
{foreach $documentTypes as $key =>$value}
documentTypesObj[counter + '_' + {$key}] = {$value};
counter++;
{/foreach}
var documentTypes = documentTypesObj;
documentTypes = documentTypes.map(d => { return d.replace(/^\d+_/g, "")});
console.log(documentTypes);
Inside var documentTypes
是带有前缀的值,我必须删除它。数值例如:
0_xxxx
1_xxxx
2_xxxx
谢谢您的建议!
你在最后加了一个'/',它必须是'/^\ d + _/g'。 –
^^^和'documentTypes = documentTypes.replace(/^\ d + _ /,'');'你必须把它赋值给变量来更新它的值并且不需要'g'标志。 – Tushar
@Tushar代码编辑 – hstur