1 syntax
1.1 Direct Volume syntax
/pattern/attributes
1.2 Creating a RegExp Object
New RegExp (pattern, attributes)
1.3 Description
Pattern is a regular expression, attributes refers to modifiers such as I,g,m
2 RegExp Object Methods
Method |
Description |
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. |
Syntax: Regexpobject.test (String) //Description: The test () method retrieves the specified value in the string. The return value is True or False var Re1 = new RegExp ("^\s+$", "G"); var msg = re1.test ("he llo"); alert (msg); Syntax: Regexpobject.exec (String) //Description: The EXEC () method retrieves the specified value in the string. The return value is the value that was found. If no match is found, then null var A is returned; var Re2 = new RegExp ("E", "G"); do { a = re2.exec ("Hello World hi!"); alert (a); } while (a! = null)
3 Methods for String objects that support regular expressions
Method |
Description |
Search |
Retrieves a value that matches a regular expression. |
Match |
Finds a match for one or more regular expressions. |
Replace |
Replace the substring that matches the regular expression. |
Split |
Splits a string into an array of strings. |
Syntax: Stringobject.search (regexp) //Description: Search () is retrieved from the beginning of the string and always returns the first matching position of Stringobject var re3 = new RegExp (" World "); var a = "Hello World". Search (Re3); alert (a); Syntax: Stringobject.match (searchvalue) //Description: The match () method can retrieve the specified value within a string, or find the match for one or more regular expressions var s = "Hi baby! hi! "; var a = S.match (/hi/); Alert (a instanceof Array);
Js RegExp Object