使用正则表达式删除以Javascript中的单词开头的行 [英] Delete line starting with a word in Javascript using regex
问题描述
我的文字很少.
随机14637547548546546546sadas3463427
随机1463754754854654654sadsa63463427
Macroflex 1463754754854654sada65463463427
随机146375475485465465sdas463463427
随机1463754754854654fdasf65463463427
Random 14637547548546546546sadas3463427
Random 1463754754854654654sadsa63463427
Macroflex 1463754754854654sada65463463427
Random 146375475485465465sdas463463427
Random 1463754754854654fdasf65463463427
我想找到以 Macroflex 开头的行(在本例中)并替换/删除它.到目前为止,这就是我所要做的...我已经多次尝试使用正则表达式,但是这使我的头部受伤了.谁能给我建议?
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', '')
推荐答案
您必须替换到该行的末尾:
You have to replace to the end of the line:
var myRegex = data.replace(/^Macroflex.*$/m, '');
请注意,您必须指定 m
标志,以使 ^
和 $
使用换行符.
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?/m, '');
因为.
与换行符不匹配.
这篇关于使用正则表达式删除以Javascript中的单词开头的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!