Home  >  Article  >  Web Front-end  >  Two methods of dynamically splicing regular expressions in js_javascript skills

Two methods of dynamically splicing regular expressions in js_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:57:252649browse

Method 1:

When working on a project, you may encounter the need to use js to verify the correctness of form input. In this case, you need to use js regular expressions. For example: For example, if you want to verify the month, the format is: 'yyyy-MM'. This regular expression is very simple to write. If you really can't write it out, you can also use Google and Baidu. There are a lot of examples on the Internet! However, if the js regular expression is written to death in this way, it will also bring new problems: What if the month format of the configuration file is changed? Change it to 'yyyyMM', or 'yyyy_MM'...? ? Do we have to remember to change the regular expressions 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 no longer need to touch the code?

The author looked through the js manual and did not find a way to convert the string into a regular expression. However, this problem can be solved indirectly by using the eval(); method to dynamically execute the script! Thus writing more versatile code!

The general solution to the above example is posted below:

Copy the code The code is as follows:

/**
* Verify whether the month form input is legal
* 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 "] for input! " "e.g "2010" splitChar "11" or "2010" splitChar "03"");
text.value = "";
text.focus();
return false;
}
return true;
}

Copy code The code is as follows:

/**
* Verify whether the month form input is legal
* 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 "] for input! " "e.g "2010" splitChar "11" or "2010" splitChar "03"");
text.value = "";
text.focus();
return false;
}
return true;
}

One thing worth noting: when dynamically spelling out a script string and passing it to the eval() method for execution, the character '' needs to be escaped

Method 2:
Copy code The code is as follows:

<script> <br> var n=new Array( ".htm ", ".html ", ".shtml "); <br>//var pattern1 = new RegExp( "\w \ " n[0] "$ ", "gi ") ; <br>var s1= "b.shtml "; <br>var result = false; <br>for(var i=0;i <n.length;i ) <BR>{ <BR>pattern1 = new RegExp( "\w \ " n[i] "$ ", "gi "); <BR>result|=pattern1.test(s1); <BR>} <BR>alert(Boolean(result)); <BR></script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn