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
vernak2539
  • 574
  • 3
  • 14

2 Answers2

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

Plynx
  • 11,341
  • 3
  • 32
  • 33
  • 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? – vernak2539 Feb 13 '13 at 02:47
  • @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` – Plynx Feb 13 '13 at 03:36
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

Community
  • 1
  • 1
kufudo
  • 2,803
  • 17
  • 19
  • 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 – vernak2539 Feb 13 '13 at 02:45