0

I have a JavaScript RegEx rule to match url. When the rule is matching, I convert the string to a link element.

This is my RegEx rule:

/^(((http(s)?|(ftp(s)?)):\/\/)(www\.)?([a-zA-Z0-9][a-zA-Z0-9\.\/-]+[a-zA-Z0-9]\.[^\s]{2,})+(\:[0-9]{5})?|(mailto:){1}([\w\.]+)\@{1}[\w]+\.[\w]{2,})\s$/gm;

When I start typing for example https://www.google.com and hit space at the end, the typed in string matches the RegEx rule and my convert logic happens.

But when I type a different string before it like this: hello world https://www.google.com and hit space, there is no match. How can I change the RegEx rule, so that it also matches the string when other strings are written before it.

webta.st.ic
  • 3,772
  • 3
  • 34
  • 78
  • 1
    Remove anchors, see https://regex101.com/r/LplryR/1. Or replace with word boundaries, `\b` (but then remove the `\s` at the end), see https://regex101.com/r/LplryR/2 – Wiktor Stribiżew Jul 01 '19 at 08:04
  • @WiktorStribiżew Awesome, thanks! Another question: I removed the first anchor `^` but keep the `$` at the end so I can trigger, when the url is finished and stop converting to link and write normal text after the link. Now after removing the first anchor, I have to hit three times spaces to insert a space and continiue writing after the link. Any ideas why? – webta.st.ic Jul 01 '19 at 08:09
  • This is not related to the current question. No idea what your code looks like and what you are doing. Your regex does not match individual spaces. – Wiktor Stribiżew Jul 01 '19 at 08:15

0 Answers0