Home > Web Front-end > JS Tutorial > How to replace regular expressions in javascript

How to replace regular expressions in javascript

coldplay.xixi
Release: 2023-01-04 09:35:40
Original
5871 people have browsed it

javascript正则表达式使用替换的方法:使用的是【replace()】方法,语法为【stringObject.replace(regexp,replacement)】。

How to replace regular expressions in javascript

本教程操作环境:windows7系统、javascript1.8.5版,DELL G3电脑。

javascript正则表达式使用替换的方法:

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

语法:

stringObject.replace(regexp,replacement)
Copy after login

如果 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>
Copy after login

Instanceof

<!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>
Copy after login

Location:

<!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>
Copy after login

Screen:

<!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>
Copy after login

相关免费学习推荐:javascript(视频)

The above is the detailed content of How to replace regular expressions in javascript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template