0

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

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this was exactly what I was looking for. For some reason I couldn't figure out how to escape everything. Regex drives me crazy, but I know I need to get better at it
2

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

2 Comments

I tried this and it worked once I deleted the $ sign at the end and the "^.+?" at the beginning. Why is that stuff at the beginning? Excuse my lack of knowledge in Regex. I know the $ means end of line. I remember being on a site that created that image you put below, but can't remember. Could you let me know?
@Vernacchia Those indicate the beginning and end of a line. But you need to remember to pass the m (multiline) option at the end of your regular expression literal so that they match line breaks. (If you're new to regular expressions, you also might not know to pass the g option, or it only replaces the first match). Regular expressions go inside slashes /.../ and the options go at the end: /.../gm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.