JavaScript REGEXP Objects
REGEXP: is a shorthand for regular expressions (regular expression).
The regular expression describes the pattern object of the character. You can use regular expressions to describe what you want to retrieve.
The simple pattern can be a single character. More complex patterns include more characters and can be used for parsing, format checking, substitution, and so on.
Grammar:
var patt=new RegExp (matching pattern, modifier);
Or: var patt=/matching mode/, matching method;
The pattern describes an expression model. The modifier (modifiers) describes whether the retrieval is global, case sensitive, and so on.
Note: When you use constructors to create regular objects, you need a regular character escaping rule (preceded by a backslash \). For example, the following are equivalent:
var re = new RegExp ("\\w+");
var re =/\w+/;
Modifiers: modifiers are used to perform case-sensitive and global matching.
Modifier description
I perform a match on case insensitive.
G performs a global match (finds all matches rather than stops after the first match is found).
M performs multi-line matching.
Square brackets: square brackets are used to find characters in a range.
Expression description
[ABC] finds any character between square brackets.
[^ABC] finds any characters that are not in brackets.
[0-9] Find any number from 0 to 9.
[A-z] finds any characters from lowercase a to lower Z.
[A-z] finds any characters from uppercase A to uppercase Z.
[A-z] finds any characters from uppercase A to lowercase z.
[ADGK] finds any character within a given collection.
[^ADGK] finds any character outside the given set.
(Red|blue|green) to find any of the specified options.
Metacharacters: metacharacters (metacharacter) are characters that have special meanings.
Metacharacters description
. Finds a single character, in addition to line breaks and line terminators.
\w find word characters.
\w Find non-word characters.
\d find numbers.
\d to find non-numeric characters.
\s Find white space characters.
\s find non-whitespace characters.
\b matches the word boundary.
\b Matches a non-word boundary.
To find the NUL character.
\ nto find newline characters.
\f Find a page break.
\ r to find the carriage return character.
\ t to find a tab.
\v Find vertical tabs.
\xxx finds characters that are specified in octal number XXX.
\XDD finds the characters specified in hexadecimal number DD.
\uxxxx finds Unicode characters that are specified in hexadecimal number xxxx.
Quantifiers
Quantifier description
n+ matches any string that contains at least one n.
n matches any string that contains 0 or more N.
N? Matches any string that contains 0 or one n.
N{x} matches a string containing a sequence of X N.
N{x,y} matches a string containing a sequence of X or Y N.
N{x,} matches a string that contains at least X N of a sequence.
n$ matches any string that ends with N.
^n matches any string that begins with N.
=n matches any string immediately following the specified string n.
?! n matches any string that does not follow the specified string n immediately thereafter.
RegExp Object Methods
Method description
Compile compile the regular expression.
EXEC retrieves the value specified in the string. Returns the found value and determines its location.
Test retrieves the value specified in the string. Returns TRUE or FALSE.
Methods for String objects that support regular expressions
Method description
Search retrieves the value that matches the regular expression.
Match finds the matching of one or more regular expressions.
Replace replaces the substring that matches the regular expression.
Split splits the string into an array of strings.
Instance code:
Example 1:
Find "W3cschool" in strings that are case insensitive
var str= "Visit W3cschool";
var patt1=/w3cschool/i;
The text of the following markup is the matched expression obtained:
W3cschool
Example 2
Full text find "is"
var str= "is the There is?";
var patt1=/is/g;
The text of the following markup is the matched expression obtained:
Is this all there are?
Example 3
Full-text lookup and case insensitive search "is"
var str= "is the There is?";
var patt1=/is/gi;
The text of the following markup is the matched expression obtained:
Is this all there are?
Common Regular Expressions form:
1. Text box can only enter a numeric code (the decimal point can not be entered)
<input onkeyup= "This.value=this.value.replace (/\d/g, ')" onafterpaste= "This.value=this.value.replace (/\D/g, ' ) ">
2. Only the number can be entered and the decimal point will be lost.
<input onkeyup= "if (IsNaN (value)) ExecCommand (' Undo ')" Onafterpaste= "if (IsNaN (value)) ExecCommand (' Undo ')" >
<input name=txt1 onchange= "if (/\d/.test (this.value)) {alert (' Input number only '); this.value= ';}" >
3. Prohibit entering letters
<input onkeyup= "This.value=this.value.replace (/[a-z]/g, ')" onafterpaste= "This.value=this.value.replace (/[A-z ]/g, ') ' ></input>
4. Only letters and kanji can be entered
<input onkeyup= "Value=value.replace (/[\d]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[\d]/g, ')) "
maxlength=10 name= "Numbers" >
5. Only English letters and numbers can be entered, not Chinese
<input onkeyup= "Value=value.replace (/[^\w\.\/]/ig, ')" >
6. Only enter numbers and English <font color= "Red" >chun</font>
<input onkeyup= "Value=value.replace (/[^\d|chun]/g, ')" >
7. Only a maximum of two digits after the decimal point (numeric, Chinese can be entered), can not enter letters and operation symbols:
<input onkeypress= "if ((event.keycode<48 | | event.keycode>57) && event.keycode!=46 | |/\.\d\d$/.test ( Value)) Event.returnvalue=false ">
8. Only a maximum of two digits after the decimal point (numbers, letters, Chinese can be entered), you can enter the operation symbol:
<input onkeyup= "This.value=this.value.replace (/^ (\-) * (\d+) \. ( \d\d). *$/, ' $1$2.$3 ') ">
9. Prohibit special characters:
Onkeypress= "if (Event.keycode < | | Event.keycode >) event.returnvalue = false;"
10. Only Chinese characters can be entered:
<input onkeyup= "Value=value.replace (/[^/u4e00-/u9fa5]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^
/u4e00-/u9fa5]/g, ') ' >
style= "ime-mode:disabled" forbidden Chinese Character input method
11. Only numbers can be entered:
<input onkeyup= "Value=value.replace (/[^/d]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^/d]/g, ')) ">
12. Only English and numerals can be entered:
<input onkeyup= "Value=value.replace (/[/w]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^/d]/g, ')) ">
Control input Box can only enter text or numbers, or you can not allow special characters to be entered
You are not allowed to enter the following characters: (like ^&*, etc.) <br>
<textarea rows=2 cols=20 name=comments onkeypress= "if ((Event.keycode > && event.keycode < 48) | | (Event.keycode > && Event.keycode <
65) | | (Event.keycode > && event.keycode < 97)) Event.returnvalue = false; " >
13. Disallow only Space input
Onkeyup= "Value=value.replace (//s/g, ')"
Onkeydown= "if (event.keycode==32) return false"
14. Only Chinese and English can be entered:
Onkeyup= "Value=value.replace (/[^/a-za-z/u4e00-/u9fa5]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^
/a-za-z/u4e00-/u9fa5]/g, ")"
15. Do not allow special characters and spaces to be entered:
<input id= "code" onkeypress= "Return Validatespecialcharacter ();" Onblur= "Validate (This)"/>
16. Cannot be empty
<input onblur= "if (This.value.replace (/^ +| +$/g, ') = =") alert (' cannot be empty! ') " >
17. The judging character consists of a letter and a number, an underscore, and a dot. And the beginning of the only underline and letters
/^ ([A-za-z_]{1}) ([\w]*) $/g.test (str)
JavaScript Regular Expressions