In js, RegExp objects have three methods: test (), exec (), and compile (). They can all be used to find strings, the following is a summary of their usage.
Example 1: exec ()
1,
The Code is as follows: |
Copy code |
<Script type = "text/javascript"> Var hyh = new RegExp ("w "); Document.write(hyh.exe c ("Are there www. bKjia. c0m in this sentence ?")) </Script>
|
Output result: w
2,
The Code is as follows: |
Copy code |
<Script type = "text/javascript"> Var hyh = new RegExp ("e "); Document.write(hyh.exe c ("Are there E in this sentence ?")) </Script> |
The output result is: null.
Example 2: test ()
1,
The Code is as follows: |
Copy code |
<Script type = "text/javascript"> Var hyh = new RegExp ("e "); Document. write (hyh. test ("Are there letters e in this sentence ?")); </Script>
|
The output result is true.
2,
The Code is as follows: |
Copy code |
<Script type = "text/javascript"> Var hyh = new RegExp ("e "); Document. write (hyh. test ("Are there letters E in this sentence ?")); </Script>
|
The output result is: false.
Example 3: compile ()
The Code is as follows: |
Copy code |
<Script type = "text/javascript"> Var hyh = new RegExp ("e "); Document. write (hyh. test ("there is no d in the text below, and this text contains e, haha ")); Hyh. compile ("d "); Document. write (hyh. test ("the above section contains e ")) </Script> |
The output is truefalse.