JavaScript中exec()regex方法的用途是什么?
exec()
在EXEC()方法是一个正则表达式的方法。它在字符串中搜索指定的模式,并且不同于test()regex方法,将找到的文本作为对象返回。如果没有匹配项,它将给出null作为输出。让我们详细讨论它。
示例1
在下面的示例中,通过exec()方法检查了变量模式“est”。在EXEC()的正则表达式的方法,在整个文本审议给定的图案之后,返回的图案作为对象。
<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