Scan for a string / escape a RegExp


#1

I would like to use atom’s API to search for a string within a specified range. I would like to use the scanInBufferRange method of the TextEditor.

Unfortunately the method requires a RegExp. This means that in order to use the method I would have to escape the RegExp.

There is a package to do just that. Question is: Is there a method to scan for a string instead of a RegExp built into atom? Or is there a method to escape a RegExp without requiring an additional package?


#2

You would need to use a package or write your own solution. Atom’s own underscore-plus comes with the _.escapeRegExp method. Since it’s essentially a one-liner, you could just use string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') instead of a package.