| layout | api-command | ||||||
|---|---|---|---|---|---|---|---|
| language | JavaScript | ||||||
| permalink | api/javascript/match/ | ||||||
| command | match | ||||||
| io |
|
||||||
| related_commands |
|
Command syntax
{% apibody %} string.match(regexp) → null/object {% endapibody %}
Description
Matches against a regular expression. If there is a match, returns an object with the fields:
str: The matched stringstart: The matched string's startend: The matched string's endgroups: The capture groups defined with parentheses
If no match is found, returns null.
Accepts RE2 syntax. You can enable case-insensitive matching by prefixing the regular expression with (?i). See the linked RE2 documentation for more flags.
The match command does not support backreferences.
Example: Get all users whose name starts with "A". Because null evaluates to false in
filter, you can just use the result of match for the predicate.
r.table('users').filter(function(doc){ return doc('name').match("^A") }).run(conn, callback)
Example: Get all users whose name ends with "n".
r.table('users').filter(function(doc){ return doc('name').match("n$") }).run(conn, callback)
Example: Get all users whose name has "li" in it
r.table('users').filter(function(doc){ return doc('name').match("li") }).run(conn, callback)
Example: Get all users whose name is "John" with a case-insensitive search.
r.table('users').filter(function(doc){ return doc('name').match("(?i)^john$") }).run(conn, callback)
Example: Get all users whose name is composed of only characters between "a" and "z".
r.table('users').filter(function(doc){ return doc('name').match("(?i)^[a-z]+$") }).run(conn, callback)
Example: Get all users where the zipcode is a string of 5 digits.
r.table('users').filter(function(doc){ return doc('zipcode').match("\\d{5}") }).run(conn, callback)
Example: Retrieve the domain of a basic email
r.expr("name@domain.com").match(".*@(.*)").run(conn, callback)
Result:
{ start: 0, end: 20, str: "name@domain.com", groups: [ { end: 17, start: 7, str: "domain.com" } ] }
You can then retrieve only the domain with the () selector and nth.
r.expr("name@domain.com").match(".*@(.*)")("groups").nth(0)("str").run(conn, callback)
Returns 'domain.com'
Example: Fail to parse out the domain and returns null.
r.expr("name[at]domain.com").match(".*@(.*)").run(conn, callback)
