Skip to content

Commit 8975f45

Browse files
committed
Document regular expressions in linking config
1 parent 2249bf8 commit 8975f45

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

versioned_docs/version-7.x/configuring-links.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,52 @@ const config = {
12461246
12471247
Depending on your requirements, you can use this functionality to parse and stringify more complex data.
12481248
1249+
## Matching regular expressions
1250+
1251+
If you need more complex matching logic, you can use regular expressions to match the path. For example, if you want to use the pattern `@username` to match a user's profile, you can use a regular expression to match the path.
1252+
1253+
Regular expressions can be specified between parentheses `(` and `)` in the after a param name. For example:
1254+
1255+
<Tabs groupId="config" queryString="config">
1256+
<TabItem value="static" label="Static" default>
1257+
1258+
```js
1259+
const RootStack = createStackNavigator({
1260+
screens: {
1261+
Profile: {
1262+
screen: ProfileScreen,
1263+
linking: {
1264+
path: ':username(@[A-Za-z0-9_]+)',
1265+
},
1266+
},
1267+
},
1268+
});
1269+
```
1270+
1271+
</TabItem>
1272+
<TabItem value="dynamic" label="Dynamic">
1273+
1274+
```js
1275+
const config = {
1276+
screens: {
1277+
Profile: ':username(@[A-Za-z0-9_]+)',
1278+
},
1279+
};
1280+
```
1281+
1282+
</TabItem>
1283+
</Tabs>
1284+
1285+
This will only match the path if it starts with `@` followed by alphanumeric characters or underscores. For example, the URL `/@jane` will match the `Profile` screen, but `/jane` won't.
1286+
1287+
Regular expressions are intended to only match path segments, not the entire path. So, avoid using `/`, `^`, `$`, etc. in the regular expressions.
1288+
1289+
:::warning
1290+
1291+
Regular expressions are an advanced feature. They cannot be validated to warn you about potential issues, so it's up to you to ensure that the regular expression is correct.
1292+
1293+
:::
1294+
12491295
## Advanced cases
12501296
12511297
For some advanced cases, specifying the mapping may not be sufficient. To handle such cases, you can specify a custom function to parse the URL into a state object ([`getStateFromPath`](navigation-container.md#linkinggetstatefrompath)), and a custom function to serialize the state object into an URL ([`getPathFromState`](navigation-container.md#linkinggetpathfromstate)).

0 commit comments

Comments
 (0)