我有一个包含多个逗号的字符串,字符串替换方法只会更改第一个:
var mystring = "this,is,a,test"
mystring.replace(",","newchar", -1)
Run Code Online (Sandbox Code Playgroud)
结果:"thisnewcharis,a,test"
文档指出默认值替换all,并且"-1"也表示替换all,但是不成功.有什么想法吗?
我想知道是否有一种轻量级方式我可以使用JavaScript或jQuery来嗅出文档中的特定文本字符; 说€并找到这个角色的所有实例.然后!写一个用$替换所有实例的能力.
我找到了这个首发代码片段:
var str = 'test: '';
str = str.replace(/'/g, "'");
Run Code Online (Sandbox Code Playgroud)
实质上; 我想要一个单页文档的解决方案.抓住X的所有实例并使其成为XY.只有文字字符.
这个让我难过.我想从label元素中删除"+" .这是HTML:
<label class="option" for="edit-attributes-21-33">
<input type="radio" id="edit-attributes-21-33" name="attributes[21]"
value="33" checked="checked" class="form-radio"> 4 oz, +$15.00</label>
Run Code Online (Sandbox Code Playgroud)
我从这开始
$(".option").each(function(index, value) {
$(this).text( $(this).text().replace("+", ""));
})
Run Code Online (Sandbox Code Playgroud)
这将删除"+",但也会删除输入元素.那么我试过:
$(".option").each(function(index, value) {
var oldString = $(this).html();
var newString = oldString.replace("+", "");
console.log(oldString, newString);
$(this).text(newString);
})
Run Code Online (Sandbox Code Playgroud)
这会产生一个正确的html标记字符串,但它是一个字符串,并以这种方式传回DOM.我看到另一篇帖子有同样的问题,但没有解决方案.