在本文中,我们将搜索特定模式,并且仅传递与给定模式匹配的字符串。我们将使用以下方法来实现此功能 -
在这种方法中,我们将搜索与给定模式匹配的字符串,并从细绳。 string.search() 是 JavaScript 提供的用于搜索字符串的内置方法。我们还可以在此方法中传递正则表达式或普通字符串。
str.search( expression )
str - 定义需要比较的字符串。
表达式 - 定义将与字符串进行比较的字符串表达式
这将返回字符串的索引,其中字符串已开始匹配,如果字符串不匹配,将返回“-1”。
立即学习“Java免费学习笔记(深入)”;
在下面的示例中,我们将字符串与正则表达式进行比较,如果字符串匹配则返回其索引。如果没有,将返回-1。
PhpEIP企业信息化平台主要解决企业各类信息的集成,能把各种应用系统(如内容管理系统,网上商城,论坛系统等)统一到企业信息化平台中,整个系统采用简单易用的模板引擎,可自定义XML标签,系统采用开放式模块开发,符合开发接口的模块可完全嵌入到平台;内容管理模块可自定义内容模型,系统自带普通文章模型和图片集模型,用户可以定义丰富的栏目构建企业门户,全站可生成静态页面,提供良好的搜索引擎优化;会员管理模
0
#index.html
<!DOCTYPE html>
<html>
<head>
<title>
Creating Objects from Prototype
</title>
</head>
<body>
<h2 style="color:green">
Welcome To Tutorials Point
</h2>
</body>
<script>
const paragraph = 'Start your learning journey with tutorials point today!';
// any character that is not a word character or whitespace
const regex = /(lear)\w+/g;
console.log("Index: " + paragraph.search(regex));
console.log("Alphabet start: " + paragraph[paragraph.search(regex)]);
// expected output: "."
</script>
</html>
在下面的示例中,我们创建了多个正则表达式,并在字符串上检查它们是否满足要求。
# index. html
<!DOCTYPE html>
<html>
<head>
<title>
Creating Objects from Prototype
</title>
</head>
<body>
<h2 style="color:green">
Welcome To Tutorials Point
</h2>
</body>
<script>
const paragraph = 'Start your learning journey with tutorials point today!';
// any character that is not a word character or whitespace
const regex = /(lear)\w+/g;
const regex1 = /(!)\w+/g;
const regex2 = /(!)/g;
console.log("Index: " + paragraph.search(regex));
console.log("Index: " + paragraph.search(regex1));
console.log("Index: " + paragraph.search(regex2));
console.log("Alphabet start: " + paragraph[paragraph.search(regex)]);
// expected output: "."
</script>
</html>
以上就是如何在 JavaScript 中搜索字符串中的模式?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号