JavaScript RegExp object
RegExp: is the abbreviation of regular expression.
Complete RegExp Object Reference
Please check out our JavaScript RegExp Object Reference, which provides all the properties and methods that can be used with string objects.
This manual contains detailed descriptions and examples of the usage of each property and method.
What is RegExp?
Regular expression describes the pattern object of characters.
When you retrieve some text, you can use a pattern to describe what you want to retrieve. RegExp is this pattern.
Simple pattern can be a single character.
More complex patterns include more characters and can be used for parsing, format checking, replacement, etc.
You can specify the search position in the string, the type of characters to be searched, etc.
Syntax
Or a simpler method
var patt=/pattern/ modifiers;
#Pattern describes an expression model.
Modifiers (modifiers) describe whether the search is global, case-sensitive, etc.
Note: When using the constructor to create a regular object, regular character escaping rules are required (add a backslash \ in front). For example, the following are equivalent:
var re = new RegExp("\w+"); var re = /\w+/;
RegExp Modifier The
modifier is used to perform a case-insensitive and full-text search.
i - Modifier is used to perform case-insensitive matching.
g - The modifier is used to perform a full-text search (instead of stopping at the first one found, find all matches).
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> var str = "Visit php.cn"; var patt1 = /php.cn/i; document.write(str.match(patt1)); </script> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> var str="Is this all there is?"; var patt1=/is/g; document.write(str.match(patt1)); </script> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> var str="Is this all there is?"; var patt1=/is/gi; document.write(str.match(patt1)); </script> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
test()
The test() method searches for the value specified in the string and returns true or false based on the result.
The following example searches for the character "e" from a string:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> var patt1=new RegExp("e"); document.write(patt1.test("The best things in life are free")); </script> </body> </html>
Run Example»
Click the "Run Instance" button to view the online instance
When using the constructor to create a regular object, regular character escaping rules are required (add a backslash\ in front)
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> var str = 'php'; var patt1 = new RegExp('\w', 'g'); // 有转义作为正则表达式处理 var patt2 = new RegExp('\w', 'g'); // 无转义作为字符串处理 var patt3 =/\w+/g; // 与 patt1 效果相同 document.write(patt1.test(str)) //输出 true document.write("<br>") document.write(patt2.test(str)) //输出 false document.write("<br>") document.write(patt3.test(str)) //输出 true </script> </body> </html>
Run instance»
Click the "Run instance" button to view the online instance
exec()
exec() method Retrieve the specified value in a string. The return value is the found value. If no match is found, null is returned.
The following example searches for the character "e" from a string:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> var patt1=new RegExp("e"); document.write(patt1.exec("The best things in life are free")); </script> </body> </html>
Run Example»
Click the "Run Instance" button to view the online instance