I have an issue with a code that goes like this:
There's a search form in which I can search for titles of films. The search text is sent to a back-end service which yields the resulting list. The results then are displayed in the interface in a list and the matched pattern is highlighted. Like so:
Search term: "ba"
- Back to the Future
- Batman
- Star Wars - The Empire Strikes Back
- Mortal Kombat
So, we use a simple regular expression built from the search term to highlight the text. It finds the match, and uses the matched text to build the string with the necessary style. Since we se case-insensitive matching, we can simply use the matched group and it will look correct.
However, the back-end ignores punctuation. So this should happen:
Search term: "ai"
- A.I. Artificial Intelligence
- Raiders of the Lost Ark
- Django Unchained
The issue is that "A.I. Artificial Intelligence" can't be matched by a simple /(ai)/gi
regex. I need a way to ignore the dots and then match the string without them. I can't simply replace the dots with nothing and then match the string, because as I mentioned, the search is done in the back-end so I know there is a pattern in the string already, and I'd like to avoid making a huge method with all edge cases for highlightning.
I'm trying to find an elegant solution using regex. I'd like my matched group to be "A.I" in this example. Can it be done?
Keep in mind that the same rule may apply to more punctuation symbols, so stuff like "Once Upon a Time... in Hollywood", "Star Wars: Episode VI", "Spider-Man", "Monsters, Inc.", etc, should follow the same principle.
This is the best I could come up with:
Search term: "bubbles"
Regex: /b([.,-\?!]?)u(\1)b(\1)b(\1)l(\1)e(\1)s/gi
It looks bad..