删除javascript中包含特定单词的行

Removing lines containing specific words in javascript

本文关键字:单词 包含特 javascript 删除      更新时间:2023-09-26

这个脚本应该获取一个链接列表,通过更改一些单词来转换一些链接,并消除其他包含特定字符串的链接。

第一部分没问题。第二部分我需要帮助。线路

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

即使我们用*更改+也不起作用。我使用了这里的来源(1&2)。所以,帮帮那个角落。

<!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>

我认为您需要转义正斜杠,因为您还将它们用作正则表达式分隔符。

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

假设要复制<p>中的那些行,则删除其中包含类别的行。

将您的功能更改为

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>" );
}