DEV Community

Discussion on: String.prototype.search(): a method I wish I knew about a long time ago

Collapse
 
cainrus profile image
cainrus

So you can make extra wrapper for exec / match method to simplify code in the userland.

Collapse
 
kenbellows profile image
Ken Bellows

Yeah, you definitely could do it that way, utilize myregex.exec() or myregex.match() inside of your method. Could be a nice way to encapsulate some ugly, repetitive logic, especially if you did this sort of thing all the time and wanted to write a factory function to take a regex and generate these search objects. Again, I don't really know if it's any cleaner in the end than just defining a findIndex(str, pattern) function, especially if not everyone reading your code will know about these symbols and understand how you're using them, but when I have the choice, I usually prefer dealing with native methods like str.search(pattern), since they have well-defined behavior and I know what to expect. Personal preference, I guess