The use of the JS Regular test method

Source: Internet
Author: User
Tags regular expression

Test method
Returns a Boolean value that indicates whether the pattern exists in the string being searched.

Rgexp.test (str)


JavaScript regular object creation and usage

declaring JavaScript regular expressions

The code is as follows Copy Code
var recat = new RegExp ("cat");
You can also
var recat =/cat/; Perl Style (recommended)

Learn the most commonly used test exec match search replace split 6 methods

Test checks whether the specified string exists

The code is as follows Copy Code
var data = "123123";
var recat =/123/gi;
Alert (recat.test (data)); True

Check to see if the character is present G continue to go down I case insensitive

Instance

Example
The following example illustrates the use of the test method:

The code is as follows Copy Code

<script language=jscript>
function Testdemo ()
{
try{
var re= "abc" var s=/a/
This sets the regular expression
Alert (S.test (RE))
Returns True if it is included
}catch (e) {
Alert ("Err")
}}
Testdemo ()
</script>

Example

The code is as follows Copy Code
<script language= "JavaScript" >
function Checkmobile (s) ... {
var Regu =/^[1][3][0-9] ... {9}$/;
var re = new RegExp (Regu);
return Re.test (s);
}
Checkmobile (' 13909910000 ');
</script>

Purpose: Check whether the input mobile phone number is correct, if return true by validation, false

Other related introduction

Test (String): Tests whether a string contains a match result

Regular
method in a String object
Match pattern: a regular match based on pattern and, if matched to, returns a matching result, such as not matching return null
Search (pattern) : A regular match based on pattern and, if matched to a result, returns the number of its indexes; otherwise return-1
Replace (pattern,replacement): a regular match based on pattern to replace the result with a replacement
Split (pattern): a regular split based on pattern, returning an array of partitions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.