• 欢迎来到本站,本站主要做程序员相关技术、博客、笔记,如果喜欢本网站那么赶紧使用Ctrl+D 收藏吧!
  • 如果有任何疑问,可以在下方文章下方留下你的评论~

JavaScript RegExp and Example of test() & exec()

电子书籍 6个月前 (12-15) 1次浏览 0个评论

What is regexp?

String is one of the most important data structures in programming, and the requirement of string operation is almost everywhere.

For example, judge whether a string is a legal email address.

Regular expressions are a powerful weapon to match strings. Its design idea is to use a descriptive language to define a rule for a string. If a string conforms to the rules, we think it "matches", otherwise, the string is illegal.

Therefore, we can judge whether a string is a legal email:

  1. Create a regular expression that matches email;
  2. The regular expression is used to match the user’s input to determine whether it is legal.

Creating a regular expression

var patt=new RegExp(pattern,modifiers);

OR

var patt=/pattern/modifiers;

Patterns describe an expression model.
Modifiers describe whether the retrieval is global, case sensitive, etc.

The i – modifier is used to perform case insensitive matching.
The g – modifier is used to perform a full-text search (instead of stopping the search when the first one is found, but finding all matches).

Example of i – Modifiers

Case insensitive find "runoob" in string.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>penpal</title>
</head>
<body>

<script>
var str = "Visit RUnoob";
var patt1 = /runoob/i;
document.write(str.match(patt1));
</script>

</body>
</html>

Result:
RUnoob

Example of g – Modifiers

Full text search "is".

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>pen pal</title>
</head>
<body>

<script>
var str="Is this all there is?";
var patt1=/is/g;
document.write(str.match(patt1));
</script>

</body>
</html>

Result:
is,is

Example of g and i – Modifiers

Full text search and case insensitive search for "is".

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>pen pal</title>
</head>
<body>

<script>
var str="Is this all there is?";
var patt1=/is/gi;
document.write(str.match(patt1));
</script>

</body>
</html>

Result:

Is,is,is

test()

The test() method searches for the value specified by the string and returns true or false based on the result.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>pen pal</title>
</head>
<body>

<script>
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
</script>

</body>
</html>

Result:
true

exec()

The exec() method retrieves the specified value in the string.

The return value is the value found. If no match is found, null is returned.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>chinapenpal.com</title>
</head>
<body>

<script>
var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));
</script>

</body>
</html>

Result:
e


中国笔友, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:JavaScript RegExp and Example of test() & exec()
喜欢 (0)
发表我的评论
取消评论
表情 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址