Home  >  Article  >  Web Front-end  >  Js regular expression knowledge summary_javascript skills

Js regular expression knowledge summary_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:29:431813browse

Regular expression:

1. What is RegExp? RegExp is the abbreviation of regular expression. RegExp objects are used to specify the content to be retrieved from text.

2. Define RegExp: var variable name=new RegExp();

3. The RegExp object has 3 methods:

1) test() retrieves the specified value in the string, and the return value is true or false.

Copy code The code is as follows:

var p1=new Reg("e");
document.write(Reg.test("welcome to China!"));

2) exec()

Copy code The code is as follows:

var p1=new Reg("e");
document.write(Reg.exec("welcome to China!"));

3) compile()

Copy code The code is as follows:

var p1=new Reg("e");
document.write(Reg.compile("welcome to China!"));

4. Modifier

1) i performs case-insensitive matching

2) g performs global matching

3) m performs multi-line matching

5. Square brackets (used to find characters within a certain range)

1) [abc] Find any characters between square brackets

2) [^abc] finds any characters not between square brackets

3) [0-9] Find any number between 0-9

4) [a-z] Find any lowercase character between a-z

5) [A-Z] Find any uppercase character between A-Z

6) [A-z] Find any character between uppercase A-lowercase z

7) [adgk] Find any character within the given set

8) [^adgk] Find any character outside the given set

9) (red|biue|green) Find any given option

6. Metacharacters

1) w: Find word characters

2) W: Find non-word characters

3) d: Find numbers

4) D: Find non-numeric characters

7. Quantifier

n matches any string containing at least one n.

n* matches any string containing zero or more n.

n? matches any string containing zero or one n.

n{X} matches a string containing X sequences of n.

n{X,Y} matches a string containing X or Y sequences of n.

n{X,} matches a string containing at least X sequences of n.

n$ matches any string ending in n.

^n matches any string starting with n.

?=n matches any string immediately followed by the specified string n.

?!n matches any string that is not immediately followed by the specified string n.

[Exercise 1] Determine whether the format of the entered ID card is correct

Copy code The code is as follows:








<script><br> function test()<br> {<br> var reg=new RegExp(/^[0-9]{17}[0-9|X]$/);<!--Format of regular expression: starting with /^, ending with $/, numbers, Use square brackets for letter ranges --><br> var text=document.getElementById("shenfen").value;<!--The id of the string entered in the text box corresponds to the id of the text box--><br> //Determine whether the region is legal (use if...else) <br> if(reg.test(text))<br> {<br> alert("Input correct")<br> }<br> else<br> {<br> alert("Input error");<br> }<br> }<br> </script>

Rendering:

[Exercise 2] Determine whether the entered email format is correct

Copy code The code is as follows:







<script><br> function y()<br> {<br> var yx=new RegExp(/^[0-9|A-z|_]{1,17}@[A-z|0-9]{1,5}.(com|cn|net|org|cc)$/) ;<br> var shuru=document.getElementById("youxiang").value; <!--Define variables and call the entire function, and the variables must be written in the function--><br> if(yx.test(shuru))<!--test regular expression method--><br> {<br> alert("zhengque");<br> }<br> else<br> {<br> alert("cuowu");<br> }<br> }<br> </script>

Rendering:


Relatively speaking, regular expressions in js are a very important point. Many places need to cooperate with regular expressions, so friends must learn this content well.

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