11

I have few lines on text.

Random 14637547548546546546sadas3463427
Random 1463754754854654654sadsa63463427
Macroflex 1463754754854654sada65463463427
Random 146375475485465465sdas463463427
Random 1463754754854654fdasf65463463427

I would like to find a line what starts with Macroflex (in this case) and replace/delete it. This is what I have so far... I have tried over and over with regex, but it makes my head hurt. Can anyone give me an advice?

var myRegex = data.replace('Macroflex', '')
winseybash
  • 704
  • 2
  • 12
  • 27
Badr Hari
  • 8,114
  • 18
  • 67
  • 100

1 Answers1

24

You have to replace to the end of the line:

var myRegex = data.replace(/^Macroflex.*$/gm, '');

Note that you have to specify the m flag to let ^ and $ work with newlines.

If you want to remove the newline after the line, you can match it:

var myRegex = data.replace(/^Macroflex.*\n?/gm, '');

This works since . does not match newlines.

The /g flag enables removing multiple occurrences of the line.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Digital Plane
  • 37,354
  • 7
  • 57
  • 59