I am trying to modify the following regex (in javascript) from the glossarizer plugin to make it less lenient.
var regex = new RegExp("(^s*|[^!])" + this.clean(term) + "\\s*|\\,$", "i");
It is trying to retrieve a definition for a term in a JSON array of terms and definitions.
[{term: "black cat", definition: "a black cat"},
{term: "cat", definition: "meow"}]
Right now it is matching "black cat" when I pass in "cat", but I do not want it to. I want it to match something that starts at the beginning of a string or after a comma and */s
and ends in either a comma or the end of the string, so that multiple definitions can be passed in.
*Match for cat:
'cat'
' Cat '
'cat, feline'
'feline, cat , cheetah'
*Not a match for cat:
'black cat'
'Catapult'
'!cat'
I tried putting new RegExp("(^|^s*|[^!])" + this.clean(term) + "\\s*$|\\s*,", "i")
(adding pipes so it should be start of string OR comma and whitespace, and so at the end it would be whitespace followed by a comma or the end of the string, but it didn't have the desired effect (using regex101.com, but being mostly confused)
You can use this regex for matching your valid cases:
RegEx Demo