Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the test () statement in JavaScript

Shulou Source: shulou.com Published: 2022-06-03 07:27:11 10月19日 Update

This article mainly explains "how to use the test () statement in JavaScript". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the test () sentence in JavaScript".

In JavaScript, the function of the test () method is to test whether the string matches the regular expression, the syntax "RegExpObject.test (string)", the match returns true, and the mismatch returns false.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

JavaScript string is the most frequently used data type in programming, and it is necessary to operate on a string in many places, such as judging whether a string is a legal E-mail address, intercepting a specified part from a string, and so on.

Regular expression is a kind of logical formula used to match strings or special characters. the so-called logical formula is composed of some specific characters to represent some rules of special strings, which can express the filtering logic of string data.

The test () method in JavaScript tests whether the string matches the regular expression, returns true if it matches, and returns false if it doesn't match.

Syntax:

RegExpObject.test (string)

String is required. The string to detect.

Example:

Var str = "Hello World!"; var reg = / [amurg] / g; [xss_clean] (reg.test (str) + "

"); / / output: true

Description:

Call the test () method of the RegExp object r and pass it the string s, which is equivalent to this expression: (r.exec (s)! = null).

Example:

Var str= "Hello world!"; / look for "Hello" var patt=/Hello/g;var result=patt.test (str); [xss_clean] ("Returned value:" + result); / / look for "world" patt=/world/g;result=patt.test (str); [xss_clean] ("

Returned value: "+ result); / / look for" php "patt=/php/g;result=patt.test (str); [xss_clean] ("

Returned value: "+ result); thank you for reading. The above is the content of" how to use the test () sentence in JavaScript ". After the study of this article, I believe you have a deeper understanding of how to use the test () statement in JavaScript. The specific use of the situation also needs to be verified by practice. Here, the editor will push more articles on related knowledge points, welcome to follow!

Tags: Characters strings statements methods regular expressions logic learning special formulas content data examples syntax tests legality functions addresses places objects Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB vpn Linux NVidia Microsoft