纯javascript相当于jquery的replace()
pure javascript equivalent of jquery replace()
在jquery中,我像这样使用替换
replace('old text','new text')
我如何用纯javascript做同样的事情?
这个是纯Javascript。看到这里。
var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "W3Schools");
replace是javascript中字符串对象的一个方法:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace
你列出的语法是纯javascript,即
string.replace(regexp/substr,newstring)
可以使用
string.replace(regexp/substr,newstring)
为例:<script type="text/javascript">
var str="Visit Microsoft!";
document.write(str.replace("Microsoft", "Stackoverflow"));
</script>
使用JavaScript string.replace()
var str="fooz bard";
alert(str.replace("foo", "baz")); //bazz bard
返回一个新字符串,其中包含被替换的模式的部分或全部匹配。模式可以是字符串或RegExp,替换可以是每次匹配时调用的字符串或函数。
'string'.replace
是原生javascript方法。可以直接用
console.log( 'string'.replace('ing', 'wow') ); // 'strwow'
如果你想用另一个dom元素替换一个元素,有一个简单的方法:
oldElem.parentNode.replaceChild(newElem, oldElem);
相关文章:
- How can I get and replace this in javascript ( jquery?
- 通过 jQuery Find and Replace 将文本 URL 与 标记包装在一起
- 如何在jquery中使用replace和regex在单词的大写字符前插入空格
- jquery中的.replace 方法
- Jquery ajax html replace
- 为什么使用 jQuery .replace 会导致网页找不到错误
- JQuery - 随机 .replace() 错误
- javascript .replace and jQuery .map
- jQuery href replace function not updatting
- Performance of Javascript replace vs jQuery replace
- 在 jQuery 中的 replace With 上添加 SlideUp 和 Slidedown 动画
- jQuery / Javascript String replace
- jQuery Find and Replace正在挂断浏览器!数据大小太大
- Jquery replace无法处理整个输入文本
- jQuery.attr('src')replace在FF中不工作
- Jquery无法调用未定义的方法replace
- 试图通过jQuery / replace()删除括号内的锚文本
- jquery .replace(/.)不是为我工作,而是为别人工作
- jQuery attr replace on aspx
- jquery replace YouTube iFrame