1: How to create regular expressions
1. Text format, the usage method is as follows:
/pattern/flags (i.e.: /mode/mark)
The constructor, the usage method is as follows:
new RegExp("pattern"[,"flags"])(ie: new RegExp("pattern"[,"mark"]))
Parameters:
pattern: text that represents regular expression
flags (mark): If this item is specified, flags can be one of the following:
g:g:global match (full match)
i:ignore case (ignore case)
gi: both global match and ignore case (match all possible values, and ignore case)
Note: Do not use quotes to mark the parameters in the text format, and do not use quotes to mark the parameters of the Ergou Crafting Function. So the following expression
It is equivalent:
/ab+c/i ==================== new RegExp("ab+c","i")
describe:
When creating regular expressions using constructor functions, it is necessary to use normal strings to avoid rules (include leading characters\ in the string).
For example, the following two statements are equivalent:
re=new RegExp("\\w+");
re=/\w+/
Note: RegExp has preset $ attribute
$1, ..., $9 attributes
Match substrings enclosed in parentheses, if any.
It is the property of RegExp
Static, read only
Provided in JavaScript 1.2, NES 3.0 or above
Description: Because input is a static property, not an attribute of individual regular expression objects. You can access this using
property.
The number of substrings that can be added with parentheses is not limited, but regular expression objects can only retain the last 9. If you want to access all
The matching string in parentheses, you can use the returned array.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <SCRIPT LANGUAGE="JavaScript1.2"> var regexp = new RegExp("(\\w+)\\s(\\w+)"); str = "John Smith"; newstr=(regexp,"$2"); newstr2=(regexp,"$1"); ("Original string:"+str+"<br/>"); (newstr+"<br/>"); (newstr2+"<br/>"); ('$1='+RegExp.$1+" $2="+RegExp.$2); </SCRIPT> </HEAD> <BODY> </BODY> </HTML>
2: The match() method can retrieve the specified value within a string, or find a match for one or more regular expressions. It returns the specified value, not the position of the string.
grammar
(searchvalue)
(regexp) Parameter Description
searchvalue Required. Specifies the string value to be retrieved.
regexp is required. RegExp object that specifies the pattern to match. If the parameter is not a RegExp object, you need to first pass it to the RegExp constructor and convert it to a RegExp object.
Return value
Stores the matching result array. The contents of this array depend on whether regexp has the global flag g.
illustrate
The match() method retrieves the string stringObject to find one or more text matching regexp. The behavior of this method depends to a large extent on whether regexp has flag g.
If regexp does not have flag g, the match() method can only perform a match once in stringObject. If no matching text is found, match() returns null. Otherwise, it returns an array that stores information about the matching text it found.
Example of match usage:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <SCRIPT LANGUAGE="JavaScript1.2"> var str="1 plus 2 equal 3"; var str2="11/23/55"; var results=(new RegExp("\\d+","gi")); for(var i=0;i<;i++){ (results[i]+"<br/>"); } var res=(new RegExp("(\\d\\d?)/(\\d\\d?)/(\\d\\d)")); if( == res[0].length){ (res[1]+"<br/>"); (res[2]+"<br/>"); (res[3]+"<br/>"); } </SCRIPT> </HEAD> <BODY> </BODY> </HTML> function dateCheck(value) { re = new RegExp("(\\d\\d?)/(\\d\\d?)/(\\d\\d)"); var result = (re); if (result){ if (result[0].length != ){ alert ("Wrong date format. The correct format should be MM/dd/yy.") return false; }else{ var t = result[3]; var y = parseInt("20" + t); var m = parseInt(result[1], 10) - 1; var day = parseInt(result[2], 10); var d = new Date(y, m, day); if (() != y || () != m || () != day){ alert ("error date!") return false; }else{ var sm = result[1].length == 1?'0' + result[1]:result[1]; var sday = result[2].length == 1?'0' + result[2]: result[2]; var sy = result[3]; else return sm + '/' + sday + '/' + sy; } } }else{ alert ("Wrong date format. The correct format should be MM/dd/yy."); return false; } }