PHP preg_replace 在 javascript
PHP preg_replace in javascript
我曾在 PHP 中工作 preg_replace,但我不得不将其移至 JavaScript 代码,但它在那里不起作用。我在这里搜索主题,但没有找到适合我的解决方案......
我的 PHP 看起来像:
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);
var documentTypes
内是带前缀的值,我必须将其删除。值例如:
0_xxxx
1_xxxx
2_xxxx
谢谢指教!
<script type="text/javascript">
documentTypes = '0_xxxx';
documentTypes = documentTypes.replace(/^\d+_/, '');
document.write(documentTypes);
</script>
如果是数组:
var docs = ["0_xxx", "1_yyy", "2_zzzz"];
docs = docs.map(d => { return d.replace(/^\d+_/g, "")});
不确定您的输入字符串,问题可能是因为您有换行或在字符串中携带 return。如果是这样,您正在寻找的解决方案可能是这样的:
var documentTypes = '0_xxxx\n\
1_xxxx\n\
2_xxxx';
documentTypes = documentTypes.replace(/(^\d+_|[\r\n]+\d+_)/gm,'');
我曾在 PHP 中工作 preg_replace,但我不得不将其移至 JavaScript 代码,但它在那里不起作用。我在这里搜索主题,但没有找到适合我的解决方案...... 我的 PHP 看起来像:
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);
var documentTypes
内是带前缀的值,我必须将其删除。值例如:
0_xxxx
1_xxxx
2_xxxx
谢谢指教!
<script type="text/javascript">
documentTypes = '0_xxxx';
documentTypes = documentTypes.replace(/^\d+_/, '');
document.write(documentTypes);
</script>
如果是数组:
var docs = ["0_xxx", "1_yyy", "2_zzzz"];
docs = docs.map(d => { return d.replace(/^\d+_/g, "")});
不确定您的输入字符串,问题可能是因为您有换行或在字符串中携带 return。如果是这样,您正在寻找的解决方案可能是这样的:
var documentTypes = '0_xxxx\n\
1_xxxx\n\
2_xxxx';
documentTypes = documentTypes.replace(/(^\d+_|[\r\n]+\d+_)/gm,'');