本文摘自PHP中文网,作者coldplay.xixi,侵删。
javascript正则表达式使用替换的方法:使用的是【replace()】方法,语法为【stringObject.replace(regexp,replacement)】。

本教程操作环境:windows7系统、javascript1.8.5版,DELL G3电脑。
javascript正则表达式使用替换的方法:
正则表达式替换使用的是replace()方法。Replace()方法是用一些字符途欢另一些字符
语法:
1 | stringObject.replace(regexp,replacement)
|
如果 regexp 具有全局标志 g,那么 replace() 方法将替换所有匹配的子串。否则,它只替换第一个匹配子串。
replacement 可以是字符串,也可以是函数。如果它是字符串,那么没有匹配都将由字符串替换。但是 replacement 中的 $ 字符具有特定的含义。举例:
1 2 3 4 5 6 | <script>
var str="5=a,6=b,7=c";
str=str.replace(/(\d+)=(\w)/g,"$2=$1");
console.log(str);
</script>
|
Instanceof
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
var str="abc";
console.log(str instanceof Array);
var arr=[];
console.log(arr instanceof Array);
</script>
</body>
</html>
|
Location:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" value="按钮" id="btn"/>
<script>
var btn=document.getElementById("btn");
btn.onclick=function(){
window.location.href="04键盘事件练习.html";
}
</script>
</body>
</html>
|
Screen:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
console.log(screen.width);
console.log(screen.height);
console.log(screen.availHeight);
console.log(screen.availWidth);
</script>
</body>
</html>
|
相关免费学习推荐:javascript(视频)
以上就是javascript正则表达式如何替换的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
javascript中字母如何转为数字
实例教程介绍javascript十大取整方法
javascript如何实现定时跳转页面
href怎么调用javascript
360浏览器不能运行javascript怎么办
如何实现网页标题闪烁效果
十款面向前端开发人员的chrome扩展(分享)
初探埋点系统
javascript中$是什么意思
javascript是编译语言吗
更多相关阅读请进入《javascript》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » javascript正则表达式如何替换