Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to functions commonly used in js regular expressions

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report