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', '')

1 Answer 1

26

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.

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

5 Comments

and use /gm instead of /m if you have multiple occurrences of the line
Sweet, now my comments work in MathJax: saturnapi.com/fullstack/equations-and-matrices
How about ignoring spaces which the line might be indented with?
I noticed that if the last line is removed, an empty line is left in there
If you're using Window like me, replace \n in the regex string with \r\n.

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.