exec() regex 方法在 JavaScript 中有什么用?
exec()
exec() 方法是 regex 方法。 它搜索字符串以查找指定的模式,并且与 test() regex 方法不同,它将找到的文本作为对象返回。如果未找到匹配项,则会将 null 作为输出返回。我们来详细讨论一下。
示例 1
在以下示例中,通过 exec() 方法检查了变量模式“est”。exec() regex 方法在整个文本中仔细检查了给定的模式后,将模式作为 对象返回。
<html>
<body>
<script>
var obj = /est/.exec("Tutorix is the best e-learning platform");
document.write(
"The object is " + obj[0] + " and its position is " + obj.index);
</script>
</body>
</html>输出
The object is est and its position is 16
示例 2
在以下示例中,通过 exec() 方法检查了变量模式“x”。exec() regex 方法在整个文本中仔细检查了给定的模式后,将模式作为 对象返回。
<html>
<body>
<script>
var obj = /x/.exec("Tutorix is the best e-learning platform");
document.write(
"The object is " + obj[0] + " and its position is " + obj.index);
</script>
</body>
</html>输出
The object is x and its position is 6
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP