js如何使用正则表达式

 时间:2026-02-16 06:23:05

1、replace()替换目标字符串,在一个长串中替换子串为另外一个字符串。

代码如下:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Test re in js</title>

</head>

<body>

repalce String:"js"

Where is the js!

<p id="position">

<button onclick="myFunction()">Search</button>

<script>

function myFunction() {

    var str="Where is the js!";

    var target="js";

    var pattern="baidu";

    var pos=str.replace(target,pattern);

    document.getElementById("position").innerHTML=pos;

}

</script>

</body>

</html>

js如何使用正则表达式

js如何使用正则表达式

js如何使用正则表达式

2、search()寻找目标字符串(target)返回并下标。

js如何使用正则表达式

js如何使用正则表达式

js如何使用正则表达式

3、test()用于测试模式串是否存在于目标字符串中,是返回true,否返回false

代码如下:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Test re in js</title>

</head>

<body>

Is :"js" in the text behind.

Where is the js!

<p id="position">

<button onclick="myFunction()">Search</button>

<script>

function myFunction() {

    var str="Where is the js!";

    var pattern=/js/;

    var result=pattern.test(str);

    document.getElementById("position").innerHTML=result;

}

</script>

</body>

</html>

js如何使用正则表达式

js如何使用正则表达式

js如何使用正则表达式

4、exec()用于搜索字符匹配返回字符,不存在返回null

代码如下:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Test re in js</title>

</head>

<body>

Is :"java" in the text behind.

Where is the js!

<p id="position">

<button onclick="myFunction()">Search</button>

<script>

function myFunction() {

    var str="Where is the js!";

    var pattern=/j/;

    var result=pattern.exec(str);

    document.getElementById("position").innerHTML=result;

}

</script>

</body>

</html>

js如何使用正则表达式

js如何使用正则表达式

5、如下是正则表达式的参数:

js如何使用正则表达式

  • Eclipse配置Maven工程运行jetty容器里
  • mysql数据库如何存储图片
  • sql server怎么设置id字段自增
  • php文件怎么打开,怎样运行PHP文件呢
  • 怎么在myeclipse中集成tomcat服务器
  • 热门搜索
    纳木错旅游攻略 excel怎么打分数 当归泡水喝的功效 腮红怎么用 社保卡丢失如何补办 高丽参泡水喝的功效 重置路由器密码 如何快速增高 如何预防高原反应 un拼音怎么读音声音