Nacht und Nebel
Nacht und Nebel

Reputation: 1

Removing lines containing specific words in javascript

This script is supposed to take a list of links, transform some by changing some words and eliminate others containing specific string of characters.

The first part is ok. I need help with the second. The line

    x = x.replace(/^.+/category/.+$/mg, "");

doesn't work even if we change the + with *. I used sources from here (1 & 2 ). So, help the noob.

<!DOCTYPE html>
<html>
<body>

<h3>Instert your links</h3>

input:<br>
<textarea id="myTextarea">
http://example.com/ad/123.html
http://example.com/ad/345.html
http://example.com/ad/3567.html
http://example.com/category/fashion.html
http://example.com/ad/8910.html
http://example.com/category/sports.html



</textarea>



<button type="button" onclick="myFunction()">Get clean links</button>

<p id="links"></p>

<script>
function myFunction() {
        x = document.getElementById("myTextarea").value;
        x = x.replace(/http:\/\/example.com\/ad\//g, "http://example./com/story/"); 
        x = x.replace(/\n/g,"</br>");
        x = x.replace(/^.+/category/.+$/mg, "");
    document.getElementById("links").innerHTML = x;
}
</script>

</body>
</html>

Upvotes: 0

Views: 1469

Answers (2)

gurvinder372
gurvinder372

Reputation: 68393

Assuming that you want to copy those lines in <p> remove line containing category in it.

change your function to

function myFunction() {
  x = document.getElementById("myTextarea").value;
  var lines = x.split("\n").filter( function(val){ 
    return val.indexOf( "category" ) == -1;
  });
  document.getElementById("links").innerHTML = lines.join( "<br>" );
}

Upvotes: 1

CBusBus
CBusBus

Reputation: 2359

I think you need to escape your forward slashes as you are also using them as the regex delimiter.

x = x.replace(/^.+\/category\/.+$/mg, "");

Upvotes: 2

Related Questions