# Do I need to add code that escapes the control of the regexp symbols?

For example, String subtraction

There are answers with a regexp. And there are answers without regexp.

But answers with regexp works incorrect when strings contains the regexp control symbols. Yes, author of this question make the rule that the strings contains alphanumeric + spaces only. What about common case? Do I need to add code that escapes the control of the regexp symbols? Are there a common PGCC rule about it?

# Yes, you need to add code

If you're going to inject a string in a regular expression, and there's a possibility that the regular expression is corrupted by doing so, your code needs to handle that correctly by itself. I can't think of any reason why it should be exempted from doing so.

Example:

This does happen quite frequently in JavaScript. To find a substring in a string, you may either use:

• string.indexOf(substring)
• string.search(substring)

The search method is shorter. But when a string is passed to it, it's implicitly coerced to a regular expression. Therefore, you can't use .search() if the substring may result in an invalid regular expression, and you must use .indexOf() instead.