0

I would like to parse URLs with Regular Expressions and find a pattern that matches with https://*.global.

Here is my URL test string on regex101.

Ideally, the regex would return https://app8.global instead of cover other https string.

const URL = `https://temp/"https://app8.global"https://utility.localhost/`;
const regex = /https:\/\/(.+?)\.global(\/|'|"|`)/gm;
const found = URL.match(regex);
console.log(found);

How would I manipulate the regex so it will return the https://*.global?

3
  • Do you mean to extract (.+?) part? Commented Nov 7, 2020 at 2:31
  • Right, I want to extract app1 ~ app8 Commented Nov 7, 2020 at 2:35
  • 1
    Just use /\bhttps?:\/\/([^\/]+?)\.global\b/i Commented Nov 7, 2020 at 6:22

2 Answers 2

2

First of all, you need to exclude slashes from the starting part, otherwise it'll match things from the previous url:

const regex = /https:\/\/([^\/]+?)\.global(\/|'|"|`)/gm;

Now, you can convert the weird 4 character or with a character group:

const regex = /https:\/\/([^\/]+?)\.global[\/'"`]/gm;

And now you can get the matches and trim off that last character:

const matches = URL.match(regex).map(v => v.slice(0, -1));

Then, matches would evaluate to ["https://app8.global"].

Sign up to request clarification or add additional context in comments.

3 Comments

What's the difference between the two regex-es?
The two regexes I gave are functionally equivalent, however the second is more readable in my opinion.
@Aplet123 Thanks for your answer. I added lookahead (?=) to filter tail " and '. The regular expression would be https:\/\/([^\/]+?)\.global(?=[\/'"])`.
1

Using Group RegExp.$1

const URL = `https://temp/"https://app8.global"https://utility.localhost/`;
const regex = /(https:\/\/([^\/]+?)\.global[\/'"`])/;
const found = URL.match(regex);
console.log(RegExp.$1);

3 Comments

if we put 3 slashes in url .which is wrong eg:https:///t6app1.global/ right string is 2 slashes exept 3rd slash consider whole string t6app1.global check regex in 101regex website to better understand.
I understand the literal use but I'm wondering how https://temp/" part is excluded.
reason is next part of u asked in regex u can see .global is matter , u want to consider whole string but this part does not match temp" with .global string so why is excluded

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.