Javascript 正则表达式从select在li中创建级别

Javascript 正则表达式从select在li中创建级别,javascript,jquery,regex,Javascript,Jquery,Regex,这是我的问题。我有一个选择字段 <select id="category_id" onchange="return showCategory(this);" name="data[category_id]"> <option value="Agencies">More > Agencies</option> <option value="Catering">More> Agencies > Catering</opt

这是我的问题。我有一个选择字段

<select id="category_id" onchange="return showCategory(this);" name="data[category_id]">
  <option value="Agencies">More > Agencies</option>
  <option value="Catering">More> Agencies > Catering</option>
  <option value="Freelancers">More > Agencies > Freelancers</option>
  <option value="Driving-Instructors">More > Agencies > Freelancers > Driving Instructors</option>
  <option value="Interior-Designer">More > Agencies > Freelancers > Interior Designer</option>
  <option value="Swimming-Instructor">More > Agencies > Freelancers > Swimming Instructor</option>
</select>

更多>机构
更多>机构>餐饮
更多>机构>自由职业者
更多>机构>自由职业者>驾驶教练
更多>机构>自由职业者>室内设计师
更多>机构>自由职业者>游泳教练
我现在做的是,把每一个都拉到一个单独的无序列表中。看起来是这样的

<li class="listCat">
            <a href="#">More</a>
            <ul class="More">
                <li></li>

                <li class="More listed_nav">
                    <a href="#">More</a>
                </li>

                <li></li>

                <li class="More listed_nav">
                    <a href="#">More &gt; Agencies</a>
                </li>

                <li></li>

                <li class="More listed_nav">
                    <a href="#">More &gt; Agencies &gt; Catering</a>
                </li>

                <li></li>

                <li class="More listed_nav">
                    <a href="#">More &gt; Agencies &gt; Freelancers</a>
                </li>
            </ul>
        </li>   
  • 下面是完成任务的JS

     $('#category_id option').not(':first-child').each(function(){
           var stripper =  $(this).text();
           var length = $(this).text().split(' ').length;
           var single;
            if(stripper.indexOf('>') === -1){
                //var str = stripper.replace(/[^a-zA-Z 0-9]+/g, '');
                    single = stripper;
                    $('.parentCat .styled_list').append('<li class="listCat "><a href="#">'+single+'</a><ul class="'+single+'"></ul></li>');
    
                }
    
        });
        $('div.parentCat ul li').each(function(){
            var liName =  $(this).text();
                $('#category_id option').each(function(){
                    var linkAdd = 'http://vizirs.com.sg/listings/index/category/category:'
                    var stripper =  $(this).text();
    
    
                    //console.log(stripper.indexOf().text()).length);
                    if (stripper.indexOf(liName) >= 0){
    
                        stripper.split(' ');
    
                        //stripper = stripper.replace(/[^a-zA-Z0-9\.]+/g," ");
                        stripper = stripper.replace(stripper,'<li class="'+liName+' listed_nav"><a href="#">'+stripper+'</a></li>');
                        var newstripper=stripper.slice(1,stripper.length); 
    
    
                        $('ul.'+liName).append('<li>'+stripper+'</li>');
    
                    }
                });
                $(this).find('.listCat ul').each(function(){
                    var ulName = $(this).attr('class');
                    console.log(ulName);
                });
        });
    
    $(“#category_id option”)。不是(“:first child”)。每个(函数(){
    var stripper=$(this.text();
    变量长度=$(this).text().split(“”).length;
    var单一;
    if(剥离器索引OF('>')=-1){
    //var str=剥离器。替换(/[^a-zA-Z 0-9]+//g');
    单个=剥离器;
    $('.parentCat.styled_list')。追加('
    • ); } }); $('div.parentCat ul li')。每个(函数(){ var liName=$(this.text(); $('#category_id option')。每个(函数(){ var linkAdd=http://vizirs.com.sg/listings/index/category/category:' var stripper=$(this.text(); //log(stripper.indexOf().text()).length); if(汽提器指数f(liName)>=0){ 剥离器分离(“”); //汽提器=汽提器。更换(/[^a-zA-Z0-9\.]+/g,”); 卸料器=卸料器。更换(卸料器,“
    • ”); var newstripper=stripper.slice(1,stripper.length); $('ul.+liName).append('li>'+stripper+''); } }); $(this).find('.listCat ul').each(function(){ var ulName=$(this.attr('class'); console.log(ulName); }); });

      是的,幽默变量。。我需要一个正则表达式JS来查看每个选项,count>,删除第一个单词很容易,但是在>之后,我需要这个单词来创建一个新的子菜单,并将所有的子菜单都放在这个子菜单中。这是棘手的部分。我试图创建几个if语句来查看第一个单词。但我的问题是找到>,然后找到后面的单词。感谢您的帮助。谢谢

      根据以下评论更新:

      这是一个挑战,但我终于做到了:

      $('#convert').click(function(){
          $('li').each(function(){
              if($(this).html()==''){
                  $(this).remove();
              }
              else if($(this).parent().hasClass('styled_list')){
                  $(this).children('ul').find('li').each(function(){
                      if($(this).html()!=''){
                          var value=$(this).html().split(' &gt; ');
                          if(typeof value[1]!='undefined'){
                              $(this).html('<a href="#">'+value[1]+'</a>');
                              if(typeof value[2]!='undefined'){
                                  $(this).append('<ul><li><a href="#">'+value[2]+'</a></li>');
                                  if(typeof value[3]!='undefined'){
                                      $(this).append('<li><a href="#">'+value[3]+'</a></li></ul>');
                                  }
                                  else{
                                      $(this).append('</ul>');
                                  }
                              }
                          }
                          else{
                              $(this).html('<a href="#">'+value[0]+'</a>');
                          }
                      }
                  });
              }
          });
          $('.listCat ul li').each(function(){
              if($(this).siblings('li').length!=0 && $(this).children('ul').length==0){
                  if($(this).children('a').text().indexOf($(this).parent().attr('class'))>=0){
                      $(this).remove();
                  }
                  if($(this).next('li').children('a').text().indexOf($(this).children('a:first').text())>=0){
                      $(this).remove();
                  }
              }
              if($(this).children('ul').length!=0 && $(this).next('li').children('a').text().indexOf($(this).children('a').text())>=0){
                  var that=this;
                  $(this).children('ul').children('li').each(function(){
                      var value=$(this).html();
                      $(that).next('li').children('ul').prepend('<li>'+value+'</li>');
                  $(this).parent().remove();
                  });
                  if($(this).next('li').children('a').text().indexOf($(this).children('a:first').text())>=0){
                      $(this).remove();
                  }
              }
          });
          $('ul.More li').each(function(){
              if($(this).children('a').text().indexOf('>')>=0){
                  $(this).children('a').text($(this).children('a').text().replace(/\n/g,' '));
                  var value=$(this).children('a').text().split(' > ');
                  $(this).children('a').text(value[0]);
                  $(this).append('<ul><li><a href="#">'+value[1]+'</a></li></ul>');
              }
          });
          var content='';
          $('ul.More li').each(function(){
              if($(this).children('a:first').text()=='Freelancers'){
                  if($(this).children('ul').length==0){
                      $(this).remove();
                  }
                  else{
                      content+=$(this).children('ul').html();
                      $(this).remove();
                  }
              }
          });
          $('ul.More li:first').children('ul').children('li:first').append('<li><a href="#">Freelancers</a><ul>'+content+'</ul></li>');
      });
      
      $(“#转换”)。单击(函数(){
      $('li')。每个(函数(){
      如果($(this.html()=''){
      $(this.remove();
      }
      else if($(this.parent().hasClass('styled_list')){
      $(this).children('ul').find('li').each(function(){
      如果($(this.html()!=“”){
      var值=$(this.html().split(“”);
      if(值的类型[1]!=“未定义”){
      $(this.html(“”);
      if(值的类型[2]!='undefined'){
      $(this.append(“
      • ”); if(值的类型[3]!='undefined'){ $(this.append(“
      ”); } 否则{ $(this.append(“”); } } } 否则{ $(this.html(“”); } } }); } }); $('.listCat ul li')。每个(函数(){ if($(this.sibbines('li').length!=0&&$(this.children('ul').length==0){ if($(this.children('a')).text().indexOf($(this.parent().attr('class'))>=0){ $(this.remove(); } if($(this.next('li')).children('a').text().indexOf($(this.children('a:first').text())>=0){ $(this.remove(); } } if($(this.children('ul').length!=0&&$(this.next('li')).children('a').text().indexOf($(this.children('a').text())>=0){ var=这个; $(this).children('ul')。children('li')。each(function(){ var value=$(this.html(); $(that).next('li').children('ul').prepend('
    • '+value+'
    • '); $(this.parent().remove(); }); if($(this.next('li')).children('a').text().indexOf($(this.children('a:first').text())>=0){ $(this.remove(); } } }); $('ul.More li')。每个(函数(){ if($(this).children('a').text().indexOf('>')>=0){ $(this).children('a').text($(this).children('a').text().replace(/\n/g')); var value=$(this.children('a').text().split('>'); $(this).children('a').text(值[0]); $(this.append(“
      ”); } }); var内容=“”; $('ul.More li')。每个(函数(){ if($(this).children('a:first').text()=='freelopers'){ if($(this).children('ul').length==0){ $(this.remove(); } 否则{ content+=$(this.children('ul').html(); $(this.remove(); } } }); $('ul.More li:first')。children('ul')。children('li:first')。append('
      • '+content+'
    • '); });
      你能提供一把小提琴吗?这很难演示