Javascript - Remove line if search text is found


Javascript - Remove line if search text is found

CODE
<!--JQuery Reference Library--> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> <script> function removeLineIfCustomTextIsFound(){ inputText = "This is an Apple\nThis is a Banana\nThis is an Orange"; // Provide the input text here. var customValue1 = "apple".toLowerCase(); //Provide the searchtext. var lines = inputText.split("\n"); //Split the input based on the new line character. var resultArray = []; //Loop through the splitted lines. $.each(lines, function(){ if(String(this).toLowerCase().indexOf(customValue1) == -1) //Check if the line contains searchtext. If yes, do not add it to the result array. resultArray.push(String(this)); }); var result = resultArray.join("\r\n"); //Join all the array elements with a new line character. alert(result); } removeLineIfCustomTextIsFound(); //OUTPUT //This is a Banana //This is an Orange </script>

Comments