-1

I'm using this JS regex text.match(/|(https:.+?path.+?)|/)[1] to get a regex of a URL that is in between pipe | characters but it's not working.

The text is ||https://url.com/path/value|| but I can't seem to extract the URL from it. I need to have path in the middle to identify this particular URL since there are other URLs in the file.

It doesn't have to be a URL that I'm extracting. I mainly would like to know how to extract something from between a pair of characters (| in this case).

user779159
  • 9,034
  • 14
  • 59
  • 89
  • Maybe this is what you are looking for? `"||https://url.com/path/value||".match(/\|(https:.+?path\/(.+?))\|/)[2]` **Output:** `value`. Here is a [**JsFiddle Demo**](https://jsfiddle.net/6cwyvchm/) – NewToJS Nov 05 '17 at 20:23

2 Answers2

0

To grab everything between the two sets of || then you could use this regex:

text.match(/\|\|(.*)\|\|/)

The first part \|\| matches the characters || literally.

The next part (.*)matches any character zero or more and groups the result.

The last part \|\| matches the closing characters || literally.

Jackson
  • 3,476
  • 1
  • 19
  • 29
0

You need to escape the pipe ("|") characters:

text.match(/\|(https:.+?path.+?)\|/)[1]

Pipe is a special character that basically means "or". https://www.regular-expressions.info/alternation.html