Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Introduction to functions commonly used in js regular expressions

Shulou Source: shulou.com Published: 2022-06-02 05:43:44 10月05日 Update

This article mainly introduces "introduction of common functions of js regular expressions". In daily operation, I believe many people have doubts about the introduction of common functions of js regular expressions. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "introduction to common functions of js regular expressions". Next, please follow the editor to study!

1. The usage of replace function of js regular expression:

Function function: the replace function returns a copy of the string after text replacement according to the regular expression.

Function format: stringObj.replace (rgExp, replaceText)

Parameters: string stringObj,rgExp regular expression, what is replaced by replaceText

The contents of this module include string creation, regular expression implicit object creation, regular expression creation, and replace method matching.

Sample code:

/ / the contents of the string to be replaced var objStr=new String ("Designed By Androidyue"); / / implicitly create a regular expression object var reG=/e/w?/g;// global match (g is the global match parameter), match e or e? The content var re=/e/w?/;// does not specify a parameter, and the value matches once with (document) {write ("string before matching" + objStr+ "

"); write (" string with global matching "+ objStr.replace (reG," * ") +"

"); write (" match, no parameter value specified for a match "+ objStr.replace (re," × ");}

II. The usage of test function of js regular expression

Function description: the return value of this method is a Boolean value, through which you can match whether the string exists in the regular expression matching result. If there is a match, return ture. If there is no match, return false. This method is often used to judge the validity of user input data, such as verifying the legitimacy of Email.

Basic syntax:

ObjReg.test (objStr)

ObjReg required RegExp object name

The string to be matched by the objStr

Sample code:

/ * Designed By Androidyue*/ / * function: check the validity of Email address * / function checkEmail () {/ / get the information var objStr=document.getElementById ("email") entered by the user in the text box. Value; / / set the regular expression var objReg=//w+ [@] {1} / w + [.] / objStr that matches Email; / / [xss_clean] (objStr) / / if you judge whether there is a match in the string, if there is a correct message, the error if (objReg.test (objStr)) {alert ("the Email address is legal!");} else {alert ("the Email address is illegal!");}} enter the Email address:

Tags: Character string expression regular content function method result array global function commonly used legal location parameter address object learning output legitimacy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Technology Redmi MariaDB Microsoft