Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.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,我需要在以破折号开头的每一行周围包装一个标记,去掉破折号,然后将整个列表包装在一个标记中 <div class="json-content"> This line doesn't have a dash This line also doesn't have a dash -An unordered list should start here because the line starts with a dash -This line starts with

使用下面的html,我需要在以破折号开头的每一行周围包装一个
  • 标记,去掉破折号,然后将整个列表包装在一个
    标记中

    <div class="json-content">
    This line doesn't have a dash
    This line also doesn't have a dash 
    -An unordered list should start here because the line starts with a dash
    -This line starts with a dash and has some dashes-in-the-middle
    -Another line that starts with a dash
    And another a line that doesn't have a dash.
    </div>
    
    
    这条线没有破折号
    这条线也没有破折号
    -无序列表应该从这里开始,因为该行以破折号开始
    -这条线以破折号开始,中间有一些破折号
    -另一行以破折号开头
    还有一条线没有破折号。
    
    我使用下面的代码()来实现这一点,但是当div中有其他非虚线文本时,它也会在这些行周围添加一个
  • 标记。我知道问题的一部分是它首先剥离了破折号,但是我试着用一个正则表达式测试仪来修改它,我就是弄不明白。我怎样才能做到这一点

     $('.json-content').each(function() {
          var $this = $(this);
    
          $this.html(
            $this
              .html()
              .trim()
              .replace(/^-|[\r\n]+-/g, "\n")
              .replace(/[^\r\n]+/g, '<li class="item">$&</li>')    
              );
              $(this).find('li.item').wrapAll( "<ul class='json-list' />");  
        });
    
    $('.json content')。每个(函数(){
    var$this=$(this);
    $this.html(
    美元这个
    .html()
    .trim()
    .替换(/^-|[\r\n]+-/g,“\n”)
    .replace(/[^\r\n]+/g,“
  • $&
  • ”) ); $(this.find('li.item').wrapAll(
      ); });
    这是一把小提琴:

    最终结果应如下所示:

    这条线没有破折号
    这条线也没有破折号

    • 无序列表应该从这里开始,因为该行以破折号开始
    • 这条线以破折号开始,中间有一些破折号
    • 另一行以破折号开头
    还有一条线没有破折号


    可以附加连字符
    ^[\t]*-.
    ,以匹配以hypen开头的行。然后在捕获组中捕获列表项,并将0+乘以一个空格字符和一个连字符

    (<li class="item">)\s*-
    
  • )\s*-
  • 在替换中,请使用空字符串

    您的代码可能如下所示:

    $('.json-content').each(function () {
        var $this = $(this);
        $this.html(
            $this
                .html()
                .trim()
                .replace(/^[ \t]*-.*/gm, '<li class="item">$&</li>')
                .replace(/(<li class="item">)\s*-/g, "$1")
        );
        $(this).find('li.item').wrapAll("<ul class='json-list' />");
    });
    
    $('.json content')。每个(函数(){
    var$this=$(this);
    $this.html(
    美元这个
    .html()
    .trim()
    .替换(/^[\t]*-.*/gm,
  • $&
  • ) .替换(/(
  • )\s*-/g,“$1”) ); $(this.find('li.item').wrapAll(
      ); });
  • $('.json content')。每个(函数(){
    var$this=$(this);
    $this.html(
    美元这个
    .html()
    .trim()
    .替换(/^[\t]*-.*/gm,
  • $&
  • ) .替换(/(
  • )\s*-/g,“$1”) ); $(this.find('li.item').wrapAll(
      ); });
      
      这条线没有破折号
      这条线也没有破折号
      -无序列表应该从这里开始,因为该行以破折号开始
      -这条线以破折号开始,中间有一些破折号
      -另一行以破折号开头
      一条线不是从短跑开始,而是中间有一条。
      
      您可以组合:

      $('.json content').html(函数(idx,oldhtml){
      返回oldhtml.replace(/\n*-([^\n]*)/g,函数(匹配,p1){
      返回“
    • ”+p1+”
    • ”; }) }).find('li').wrapAll(
        
        这条线没有破折号
        这条线也没有破折号
        -无序列表应该从这里开始,因为该行以破折号开始
        -这条线以破折号开始,中间有一些破折号
        -另一行以破折号开头
        还有一条线没有破折号。
        
        谢谢!我对此进行了测试,它非常棒,除非在一行中有一个连字符不是以连字符开头。看这把小提琴的最后一行。它删除了第一个连字符后面的单词:@LBF几分钟前我已经更新了帖子。现在应该没事了。看,谢谢!更接近了,但它将最后一行包装在一个li标记中。那一行不是以连字符开头的,所以它不应该是列表项。@LBF啊,我明白了,现在它应该是正确的。我已经更新了我的答案,谢谢。看到正则表达式变化的进展帮助我更多地了解了它是如何工作的。Regex一直让我困惑,但我开始理解它的模式。再次感谢。