Title: New tweet urls regex for correct character counting
Tags: javascript;regex;twitter
Question: I am trying to reproduce new tweet form behaviour such as correct character counting for urls. Therefore I need correct regex that will return array of 'true' or urls according to examples:

```1. www.google.com
2. http://www.google.com
3. https://www.google.com
4. http://google.com
5. https://google.com
6. google.com
```

My latest discovery was:

```(http|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;amp;:\/~+#-]*[\w@?^=%&amp;amp;\/~+#-])?
```

Which works almost perfect but it doesn't catch option 1 (with www at the beginning).

I don't want url like ```google.com``` to be valid when in ```jorgealvarado@example.org``` 

My goal is to be able to count all valid urls.

Comment: @krzyk you are right. I made mistake.

Comment: Why not make `google.com` a valid address? Some sites don't have the `www.` in the beginning at all (e.g. stackoverflow.com).

Comment: Please update the question to reflect that

Here is the accepted answer: I decided to use existing library from https://github.com/twitter/twitter-text/tree/master/js

It works just like on twitter website.


Here is another answer: So make the ```http://``` part optional:

```(?:(http|https):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;amp;:\/~+#-]*[\w@?^=%&amp;amp;\/~+#-])?
```

Comment for this answer: Now it includes also `google.com` from `jorgealvarado@example.org`

Here is another answer: Try this:

``` ^(https|http)?(\:\/\/)?([\w\.]*)\.([\w\.]+)
```

tested in notepad++ and regex101.com

Comment for this answer: So next time - add ALL PATTERNS you want to find:
^(https|http)?(\:\/\/)?([\w\@\.]*)\.([\w\.\@\S?\/?]+)
Comment for this answer: Now it returns only google.com and www.google.com
Tested in rubular.com
Comment for this answer: They might be in a string: 
`www.google.com http://google.com https://google.com http://www.google.com https://www.google.com/dasd?asd=asd@asd google.com jorgealvarado@example.org`

Here is another answer: I tested this in rubular.com (for Ruby):

```(?<![@\w])(((http|https)(:\/\/))?([\w\-_]{2,})(([\.])([\w\-_]*)){1,})([\w.,@?^=%&amp;amp;:\/~+#-]*[\w@?^=%&amp;amp;\/~+#-])
```

For JS:

```(^(((http|https)(:\/\/))?([\w\-_]{2,})(([\.])([\w\-_]*)){1,})([\w.,@?^=%&amp;amp;:\/~+#-]*[\w@?^=%&amp;amp;\/~+#-]*)|(?:[^@])\b(((http|https)(:\/\/))?([\w\-_]{2,})(([\.])([\w\-_]*)){1,})([\w.,@?^=%&amp;amp;:\/~+#-]*[\w@?^=%&amp;amp;\/~+#-]*))
```

Tested here: regex101

Comment for this answer: Almost. When `www.google.com` is at the beginning it returns:
`.google.com`, when http/https it returns nothing, when `google.com` it returns nothing.
Comment for this answer: Sorry for delay with answer but I am trying to make it work in my application to test it properly. I am using javascript and i get error:
`Uncaught SyntaxError: Invalid regular expression: [here your regex] : Invalid group at :1:1`
Comment for this answer: Thank you very much for your support. Unfortunately your last regex didn't work properly so I decided to use twitter-text from: https://github.com/twitter/twitter-text/tree/master/js

It works so I will stick to that for now.
Comment for this answer: Ah, I see. This is because lookbehind is not supported in Javascript. I assumed, from you testing it on rubular.com, that you are using Ruby. For javascript, you can test it here: [https://regex101.com/r/xH0uR6/1](https://regex101.com/r/xH0uR6/1) I could not avoid repeating the regex for the alteration - perhaps there is a better option, but it works.
`^(((http|https)(:\/\/))?([\w\-_]{2,})(([\.])([\w\-_]*)){1,})([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-]*)|(?:[^@])\b(((http|https)(:\/\/))?([\w\-_]{2,})(([\.])([\w\-_]*)){1,})([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-]*)`

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 4, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:06/28/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><regex><twitter>', 'Title': 'New tweet urls regex for correct character counting', 'ViewCount': 125, 'id': 38071307, 'user_AboutMe': '', 'user_AccountId': 2349613.0, 'user_DisplayName': 'Dominik Spisak', 'user_Reputation': 93.0, 'user_Views': 19.0, 'user_id': 2058845.0}}