Gid*_*ids 7 javascript regex iterator replace
我想用迭代数替换字符串中的空行
例如,替换
串:
"My first line
My second line
My third line"
同
"
1
My first line
2
My second line
3
My third line"
我可以使用匹配和替换这些行
var newstring = TestVar.replace (/(^|\n\n)/g, "\nhello\n");
但是我正在努力添加一个函数,它会为每个函数添加一个迭代数.
你能帮我吗?
TIA,
导游服务
是的,你可以在javascript中做到这一点.你只需要将一个函数作为第二个参数传递给replace.
var i = 0;
var newstring = TestVar.replace(/(^|\n\n)/g, function() { return '\n' + (++i) + '\n'; });
函数实际上获取了很多参数,您可以根据这些参数决定要替换的值,但我们不需要任何参数来完成此任务.
| 归档时间: | 
 | 
| 查看次数: | 5228 次 | 
| 最近记录: |