Hi I want to search about some characters (or word) in text using JavaScript and remove the line that starts this characters/word..

example

remove the lines that start with ( He ) ..

input

Hello world, 
He like cats and dogs
Bye bye

output

Hello world, 
Bye bye
share|improve this question

77% accept rate
5  
what have you tried so far? – Dexter Oct 30 '11 at 16:42
I found the string. but how to remove the line that starts with this word – Adham Oct 30 '11 at 16:44
1  
Is this homework? – Amir Raminfar Oct 30 '11 at 16:45
In which format you have initial string, how to judge line delimeter ? – Dev Oct 30 '11 at 16:45
@adham, show us what code you've tried so far then! – Dexter Oct 30 '11 at 16:46
show 3 more comments
feedback

closed as not a real question by Shog9 Oct 30 '11 at 18:08

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

3 Answers

up vote 2 down vote accepted

You can find the pos of the "He " using the indexOf method and then get the position of the next line-break to remove that line using the substring method.

var someString = "Hello world,\n" +
    "He like cats and dogs\n" +
    "Bye bye";

var pos = someString.indexOf('He ');
var nextLineBreak = someString.indexOf('\n', pos);

var firstPart = someString.substring(0, pos);
var secondPart = someString.substring(nextLineBreak +1);

var finalString = firstPart + secondPart;
alert(finalString);

jsFiddle here : http://jsfiddle.net/zw2Gh/

share|improve this answer
1  
+1 but it only takes care of one occurence. – pimvdb Oct 30 '11 at 16:55
feedback

Here is a single line regex based replace:

someString.replace(/^He .*(\n|$)/mg, '');

It enables the multiline and global flags (mg) which allows us to use ^ and $ to match after and before newlines. And it also will remove all occurrences of the pattern. If you want lines with just "He\n" to be replaced, you will need to make the ' .*' optional, like so:

someString.replace(/^He( .*)?(\n|$)/mg, '');
share|improve this answer
This should be marked as the answer. Best solution here. – Leo Oct 30 '11 at 21:09
feedback

You can split the string up into separate lines, throw away the lines you want, and finally join the lines back into one string:

var string = "test\nHe test\ntest"; // "\n" represents a newline

var result = string.split("\n") // split into lines
                   .filter(function(line) {              // only retain lines
                       return line.indexOf("He ") !== 0; // that don't start
                   })                                    // with "He " *
                   .join("\n"); // join the lines back into one string

*) "Doesn't start" means: the characters are not apperant at index 0 in the line.

share|improve this answer
1  
Minor note: it should be "He " not "He" – lwburk Oct 30 '11 at 16:50
@lwburk: I just noticed that, thanks. – pimvdb Oct 30 '11 at 16:51
feedback

Not the answer you're looking for? Browse other questions tagged or ask your own question.