js-正则表达式的替换

正则表达式替换使用的是replace()方法。Replace()方法是用一些字符途欢另一些字符

语法:stringObject.replace(regexp,replacement)

regexp

必需。规定了要替换的模式的 RegExp 对象。请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象。

replacement

必需。一个字符串值。规定了替换文本或生成替换文本的函数。

如果 regexp 具有全局标志 g,那么 replace() 方法将替换所有匹配的子串。否则,它只替换第一个匹配子串。

replacement 可以是字符串,也可以是函数。如果它是字符串,那么没有匹配都将由字符串替换。但是 replacement 中的 $ 字符具有特定的含义。举例:

<script>
    //"5=a,6=b,7=c"换成"a=5,b=6,c=7"
    var str="5=a,6=b,7=c";
    str=str.replace(/(\d+)=(\w)/g,"$2=$1");
    console.log(str);
</script>

Instanceof

<!DOCTYPE html>
<html>
<head lang="en">
    <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:

<!DOCTYPE html>
<html>
<head lang="en">
    <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:

<!DOCTYPE html>
<html>
<head lang="en">
    <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>

 

posted @ 2016-08-20 22:09  划过天际  阅读(30369)  评论(0编辑  收藏  举报