Reputation: 3307
How can I write a Regex to represent certain keywords without any letter/number following or preceding them? (symbols and spaces are optional)
I tried to write this, but it's doesn't seem to work:
let resWord = "[class|function|static|this|return]"
let keyword = new Regex("[^[^a-zA-Z0-9]]"+resWord+"[^[a-zA-Z0-9]$]")
I'm new to Regex, so please excuse me if it's a stupid question :)
Upvotes: 6
Views: 16101
Reputation: 6055
This can be accomplished using negative look aheads and behinds
(?<![a-zA-Z0-9])(class|function|static|this|return)(?![a-zA-Z0-9])
or with out them.
([^a-zA-Z0-9])(class|function|static|this|return)([^a-zA-Z0-9])
Upvotes: 11