I started learning regular expression recently. I have a small problem statement now.
I am having a pattern like this
var pattern = /^(?:hawaian(?:beach|pizza|hotel))$/gim;
var text = "hawaian hotel is near hawaian beach. In the hawaian hotel they sell hawaian pizza";
I want to actually match hawaian hotel, hawaian beach and hawaian pizza in this text.
But the pattern is returning true only for conditions like hawaianhotel || hawaianbeach || hawaianpizza.
So i tried like this
var text = "hawaianhotel is near hawaianbeach. In the hawaianhotel they sell hawaianpizza";
But this is also not working.
Only this one is working
var matches = pattern.exec('hawaian*');
Your pattern uses beginning and end of string anchors, so you would only match strings if the pattern matches the entire string. Remove the
^and$and you should be okay. Note that I also added a space so that you can match “hawaian hotel” instead of “hawaianhotel”.