-1

My Regex doesn't work :

-- Without modifiers removing "https://www." in the console works. But I want more. -- With especially groups ([a-zA-Z]+ and/or modifier (+?), my original string is not matched.

WORKS query = https://www.
DOESN'T WORK https://www.[a-zA-Z]+.
DOESN'T WORK https?://www.

Sample string :

var text = `
hi max, I have a big link to share, it's https://www.test.com/?q=what&sz=48x48. 
What do you think ?
`;

WORKS

text = text
    .replace("max","bob")
    .replace('https://www\.',"");

DOESN'T WORK

    .replace("max","bob")
    .replace('https?://www\.[a-zA-Z]+\.[a-zA-Z]+\/?',"");```

ozaarm
  • 149
  • 1
  • 1
  • 7
  • 2
    Why do you want to do this with regex? You can just parse the URL and get the parts of it you need. – VLAZ Oct 01 '20 at 15:44
  • I don't want any part of a URL. – ozaarm Oct 02 '20 at 05:44
  • "*I want to remove everything up to the TLD + optional trailing /. I don't mind keeping the rest.*" – VLAZ Oct 02 '20 at 05:45
  • I don't mind doesn't mean I want it. For my requirements I wanted to get rid of at least everything up to the TLD/, I've seen Regex that does that but that was not the point, the point was my syntax with the quotes didn't work; Anthony below pointed out that I needed to use the / / syntax. Don't know why the quote comes up so often (I'm a newbie if that wasn't clear) :-) – ozaarm Oct 02 '20 at 07:51

1 Answers1

1

Here is the right syntax:

text.replace(/https?:\/\/www\.[a-zA-Z]+\.[a-zA-Z]+\/?/,"");

Although there are other issues with this code (not all domains starts with www etc).

Anthony Garcia-Labiad
  • 3,531
  • 1
  • 26
  • 30
  • www was for testing and it's there only because [a-zA-Z]+ or [a-zA-Z]\.? didn't work for the subdomain. – ozaarm Oct 02 '20 at 05:45
  • You showed me the right / / syntax without the single quote, that was the issue, many thanks ! – ozaarm Oct 02 '20 at 05:50