0

I have written url regex in javascript

/^{[\$\w][\$\w\.\[\]]+\}|^https{0,1}:\/\/[\w\-\.:\/\$\{\}\?=+&#\~%"()]+$/

which is working fine, now i am trying to add '#do_not_shorten:' to my url regex

Could any one help me to update regex which has to match with the below condition '#do_not_shorten:http://www.myexample.com/folder1/folder2/veryverylongstringhere.html'

sekhar
  • 111
  • 1
  • 1
  • 10
  • I'm not sure I fully understood your problem, but my intuition said that you need a [negative lookbehind](https://stackoverflow.com/questions/641407/javascript-negative-lookbehind-equivalent) – Christian Vincenzo Traina Feb 12 '20 at 11:17
  • You could prepend it as a non capturing group `^{[$\w][$\w.\[\]]+\}|(?:^#do_not_shorten:)?https{0,1}:\/\/[\w\-.:\/${}?=+~%"()]+$` https://regex101.com/r/4Tz4VJ/1 – The fourth bird Feb 12 '20 at 11:21

1 Answers1

0

This may be a workaround but this worked for me.

You can create two different regex and combine them to get your desired regex

var re = new RegExp(/^{[\$\w][\$\w.[]]+}|^https{0,1}://[\w-.:/\${}\?=+&#\~%"()]+$/);

var re1 = new RegExp('#do_not_shorten');

Combining both of them to get the desired regex.

var actualRegex = new RegExp('(' + re.source + ')|(' + re1.source + ')')

Abinash
  • 203
  • 2
  • 5