vernak2539
vernak2539

Reputation: 596

How to replace Javascript multiline comments with text in middle using regex

I am trying to replace a two multiline comments (on a single line) with javascript text in the middle. I am using a build tool, which reads the entire file, and need to replace a specific string (made up of comments) during the build.

Example:

var data = /*testThisDelete:start*/new Date();/*testThisDelete:end*/

Once replaced, should used like this

var data = 4.6.88

Upvotes: 0

Views: 426

Answers (2)

kufudo
kufudo

Reputation: 2833

Try something like this to get started:

"your file as a string".replace(new RegExp('/\*testThisDelete\:start.*testThisDelete\:end\*/','m'), '"replacement text"');

See this post for a lot of useful additional info: JavaScript replace/regex

Upvotes: 2

Plynx
Plynx

Reputation: 11461

Are you looking for:

^.+?(\/\*testThisDelete:start\*\/.+?\/\*testThisDelete:end\*\/)$

With this you should just be able to replace the first matched substring with what you want.

enter image description here

Upvotes: 2

Related Questions