Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中将行包装到列表_Javascript_Jquery_Html_Regex - Fatal编程技术网

在JavaScript中将行包装到列表

在JavaScript中将行包装到列表,javascript,jquery,html,regex,Javascript,Jquery,Html,Regex,我有一个HTML文件中包含文本链接的行列表,我想将其转换为超链接和JavaScript中的li列表 例如: 链接 链接文本 链接 到 正文 此脚本将文本链接转换为超链接: 我也尝试过在每行中添加li标记: //Wrap each line with li tags $('#links').ready(function() { // Get each ul $('ul').each(function(){ //

我有一个HTML文件中包含文本链接的行列表,我想将其转换为超链接JavaScript中的li列表

例如:

    链接 链接文本 链接

  • 正文
此脚本将文本链接转换为超链接:

我也尝试过在每行中添加li标记:

//Wrap each line with li tags
       $('#links').ready(function() {
        // Get each ul
        $('ul').each(function(){
            // Get the content
            var str = $(this).html();
            // Select each lines starting with url
            //trim blank space before and include text after the links)
            var regex = /(http.*?).*/ig
            // Wrap each new line in textarea with li tags
            var replaced_text = str.replace(regex, "<li>$1</li>");
            // Echo link
            $(this).html(replaced_text);
        });
    });
//用li标记将每行包装起来
$('#links').ready(函数(){
//获得每个ul
$('ul')。每个(函数(){
//获取内容
var str=$(this.html();
//选择以url开头的每一行
//修剪链接前的空白,并在链接后包含文本)
var regex=/(http.*?.*/ig)
//用li标记在textarea中换行
var replaced_text=str.replace(regex,“
  • $1
  • ”); //回音链路 $(this).html(替换为文本); }); });
    。。。但它不起作用。我希望最好在一个JavaScript函数中进行多次替换


    提前多谢

    我宁愿这样做:

    $('ul>a').wrap("<li/>");
    
    $('ul>a')。换行(
  • );
  • 您的演示不是将纯文本转换为链接吗?您使用的是无效的HTML标记,您不应该期望它按“预期”工作。顺便说一句,你不应该使用任何正则表达式来解析HTML@KingKing是的,这个演示将纯文本链接转换为超链接,并且它可以工作
    //Wrap each line with li tags
           $('#links').ready(function() {
            // Get each ul
            $('ul').each(function(){
                // Get the content
                var str = $(this).html();
                // Select each lines starting with url
                //trim blank space before and include text after the links)
                var regex = /(http.*?).*/ig
                // Wrap each new line in textarea with li tags
                var replaced_text = str.replace(regex, "<li>$1</li>");
                // Echo link
                $(this).html(replaced_text);
            });
        });
    
    $('ul>a').wrap("<li/>");