0

I want the regex pattern to only match a9.com or www.a9.com
I have tried add ? before www. /?([www]+\.)+[\ba9\.com\b]/; but it shows error..
Did I miss anything ?

hope this result

a9.com   true
www.a9.com  true
a91.com  false
www.a91.com   false

https://jsfiddle.net/c2wsds0g/

var str = 'a9.com';

var regexPattern = /([www]+\.)+[\ba9\.com\b]/;
var result = regexPattern.test(str);
console.log(result)

only regex. not use split or other method

Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43
user1775888
  • 3,147
  • 13
  • 45
  • 65

1 Answers1

2

This Regex sould do the trick:

(www\.)?a9\.com

The ´?´ quantifier, to make the group optional goes after the group, not before.

Bonus: I always test my regex with regexpal.com, you should try it. You´ll find a handy cheat sheet there too.

ncardeli
  • 3,452
  • 2
  • 22
  • 27