1

I need to filter the text below from the string using Regex JavaScript:

Text: 'http://www.mywebsiteameW1234.com'

Should return only: mywebsitename

So between character dots and only lowercase letters:

My attempt is only returning: mywebsitenameW1234 should remove the numbers:

let text = 'http://www.mywebsitenameW1234.com'
console.log(text.match(/(?<=\.)(.*)(?=\.)/gm)[0])

I tried several ways to try to filter instead of (.*) putting something like ([a-z]+) but always return null.

What am I doing wrong? Why can't I add those filters in between groups look ahead/behind, etc..?

Roger Oliveira
  • 1,589
  • 1
  • 27
  • 55

2 Answers2

4

One expression, for instance, would be:

https?:\/\/(?:www\.)?([a-z]+).*

which is left bounded.

Demo 1


Another one would be,

([a-z]+)[^a-z\r\n]*\.[a-z]{2,6}$

Demo 2

which is right-bounded, and you could also double bound it, if that'd be necessary.


Or with lookarounds, one option would be,

[a-z]+(?=[^a-z\r\n]*\.[a-z]{2,6}$)

Demo 3

const regex = /https?:\/\/(?:www\.)?([a-z]+).*/gm;
const str = `http://www.mywebsiteameW1234.com`;
const subst = `$1`;

const result = str.replace(regex, subst);

console.log(result);

If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Emma
  • 27,428
  • 11
  • 44
  • 69
2

How about this one.

The [a-z]* expression ensures that it will only match lowercase letters after the first dot in the string.

let text = 'http://www.mywebsitenameW1234.com'
console.log(text.match(/(?<=\.)[a-z]*/gm)[0])
Joven28
  • 769
  • 3
  • 12