replace()没有按预期工作

时间:2015-07-24 13:37:14

标签: javascript

为什么" 名称"这里没有取代?



var foo = '<div>                <label class="required">_1_label__</label><div id="project_frontendbundle_car_1___name__"><div>                <label for="project_frontendbundle_car_carImages___name___position" class="required">Position</label><input type="number" id="project_frontendbundle_car_carImages___name___position" name="project_frontendbundle_car[carImages][__name__][position]" required="required" /></div><div>                <label for="project_frontendbundle_car_carImages___name___fileName">File name</label><input type="text" id="project_frontendbundle_car_carImages___name___fileName" name="project_frontendbundle_car[carImages][__name__][fileName]" maxlength="2000" /></div></div></div>';

var bar = foo.replace('_name_', '1');
$('body').text(bar);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:4)

尝试

var bar = foo.replace(/_name_/g, '1');

g =全球替换