Skip to content
Advertisement

Find and replace (part of) string in comment blocks with regex

I’m trying to find a certain string that can occur inside a comment block. That string can be a word, but it can also be part of a word. For instance, suppose I’m looking for the word “codex”, then this word should be replace with “bindex” but even when it’s part of a word, like “codexing”. This should be changed to “bindexing”.

The trick is, that this should only happen when this word is inside a comment block.

JavaScript

What I have so far is this code:

JavaScript

As you can see in this example, this isn’t really working the way I’d like. It doesn’t replace the word when it’s inside a multiline /* */ comment block, And sometimes it removes all the text that was in front of the word “codex” as well.

How can I improve my regex so that it meets my requirements?

Advertisement

Answer

Since you’re dealing with multi-line text here you should be using s modifier (DOTALL) to match text across multiple line. Also forward slash doesn’t need to be escaped.

Try this code:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement