Two Methods for js dynamic splicing of Regular Expressions _ javascript skills

Source: Internet
Author: User
This article mainly introduces two methods for js dynamic splicing of regular expressions. For more information, see Method 1:

When creating a project, you may need to use js to verify the correctness of the form input. In this case, you need to use the regular expression of js. For example, if you want to verify the month in the format of 'yyyy-mm', this regular expression is easy to write and can be written either by Google or Baidu. There are a lot of online examples! However, writing the js regular expression will also lead to a new problem: what if the month format of the configuration file is changed? Change to 'yyymm' or 'yyyy _ mm ?? Do we need to remember to change the Regular Expression in js over and over again ??

At this time, we have to ask: how can we write dynamic Regular Expressions? After modifying the configuration file, we don't need to move the Code any more?

I have read the js manual and fail to find a method to convert a string to a regular expression. However, we can use eval (); to solve this problem indirectly by dynamically executing scripts! To write more common code!

The general solution of the above example is shown below:

The Code is as follows:


/**
* Verify that the input in the month form is valid.
* Pattern: month Format String
* Id: Form id
*/
Function validateMonth (pattern, id ){
Var text = document. getElementById (id );
Var monthStr = text. value;
Var splitChar = "";
If (pattern. length> 6) splitChar = pattern. substring (4, pattern. length-2 );
Eval ("var re =/\ d {4}" + splitChar + "\ d {2} $ /;");
// Var re =/\ d {4}-\ d {2} $ /;
If (monthStr. match (re) = null ){
Alert ("please refer to the format [" + pattern + "] input! "+" E. g \ "2010" + splitChar + "11 \" or \ "2010" + splitChar + "03 \"");
Text. value = "";
Text. focus ();
Return false;
}
Return true;
}


The Code is as follows:


/**
* Verify that the input in the month form is valid.
* Pattern: month Format String
* Id: Form id
*/
Function validateMonth (pattern, id ){
Var text = document. getElementById (id );
Var monthStr = text. value;
Var splitChar = "";
If (pattern. length> 6) splitChar = pattern. substring (4, pattern. length-2 );
Eval ("var re =/\ d {4}" + splitChar + "\ d {2} $ /;");
// Var re =/\ d {4}-\ d {2} $ /;
If (monthStr. match (re) = null ){
Alert ("please refer to the format [" + pattern + "] input! "+" E. g \ "2010" + splitChar + "11 \" or \ "2010" + splitChar + "03 \"");
Text. value = "";
Text. focus ();
Return false;
}
Return true;
}


It is worth noting that when the script string is dynamically spelled out and passed to the eval () method for execution, escape the character '\'.

Method 2:

The Code is as follows:


Script
Var n = new Array (". htm", ". html", ". shtml ");
// Var pattern1 = new RegExp ("\ w + \" + n [0] + "$", "gi ");
Var s1 = "B .shtml ";
Var result = false;
For (var I = 0; I {
Pattern1 = new RegExp ("\ w + \" + n [I] + "$", "gi ");
Result | = pattern1.test (s1 );
}
Alert (Boolean (result ));
Script

Related Article

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.