Skip to content

feat request: string pattern support #82

@ridvanaltun

Description

@ridvanaltun

Hey, I did something like this to support string patterns:

import React from 'react';

import Autolink, {AutolinkProps, CustomMatcher} from 'react-native-autolink';

const escapeRegExp = (value: string) => {
  return value.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};

interface CustomMatcherInstance extends Omit<CustomMatcher, 'pattern'> {
  pattern: RegExp | string;
}

interface Props extends Omit<AutolinkProps, 'matchers'> {
  matchers: CustomMatcherInstance[];
}

const AutolinkInstance = ({matchers, ...props}: Props) => {
  return (
    <Autolink
      matchers={matchers.map(matcher => ({
        ...matcher,
        pattern:
          typeof matcher.pattern === 'string'
            ? new RegExp(escapeRegExp(matcher.pattern))
            : matcher.pattern,
      }))}
      {...props}
    />
  );
};

export default AutolinkInstance;

My aim increase the usability.
I don't know is there any issue with it but it worked so far.

What do you think? @joshswan

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions