删除特定子字符串周围的引号

时间:2016-01-19 02:30:09

标签: javascript regex

如何使用javascript中的正则表达式从HTML元素的所有属性中删除双引号? 我试过这个帖子中提供的正则表达式: Regex to remove quotes around attributes when possible

它适用于大多数属性,但是如果我有一个带有href的锚标记,例如" page2.html"或" page2.html#someid"它没有删除引号。

由于

1 个答案:

答案 0 :(得分:-1)

您可以使用:

    app.controller('questionnaire', ['$scope','$http','$sce','$routeParams',function($scope, $http, $sce, $routeParams) {

        $scope.questionAttempted = []; // an array containing question ids which are attempted
        $scope.questionAnswerAttempted=[];//an array containing answers which are checked    
        
        $scope.init=function()
        {
            $http.get('quest_answer.json').then(function(questionnaireData){      
                $scope.questionnaire = questionnaireData.data; 
             });                    
        }    
       
        
        $scope.selectAnswer = function(qIndex, aIndex, selectedAnswer)
        {         
            var selectedIndex = $scope.questionAnswerAttempted.indexOf(selectedAnswer.value);  
            var selectedQuestion= $scope.questionAttempted.indexOf(qIndex);  
           
            if(selectedIndex == -1 && selectedAnswer.isChecked){		
                $scope.questionAnswerAttempted.push(selectedAnswer.value);          
                if(selectedQuestion == -1){
                    $scope.questionAttempted.push(qIndex);                
                }
            }else if(!selectedAnswer.isChecked && selectedIndex != -1){
                $scope.questionAnswerAttempted.splice(selectedIndex,1);           
                 var  aLength = $scope.questionnaire[qIndex].answer.length;      
    				//Remove the question id from the questionAttempted array only if none of the answers are checked.
                if(aLength >0){
                       for(a=0;a<aLength;a++){
                           if($scope.questionnaire[qIndex].answer[a].isChecked){
                                console.log("Atleast one answer is checked for this question. So no of questions attempted remains same:" + $scope.questionAttempted.length);  
                               flag = false;
                               break;                           
                           }else{
                               flag = true;                                                    
                           }
                       }
                    if(flag){
                        $scope.questionAttempted.splice(selectedQuestion,1);
                              console.log("No answers are checked for this question. So removing it");   
                              console.log("No of questions attempted after splicing:" + $scope.questionAttempted.length);
                    }
                    
                }            
            }        
        }
        
    	// For ng-required attribute of checkboxes. Should return 'true' for attempted question and 'false' for unattempted question
        $scope.anySelected = function(qIndex,answer){
            var qaLength =$scope.questionAttempted.length;
              if(qaLength > 0){
                  for(var q = 0; q < qaLength ; q++){                  
                      if($scope.questionAttempted[q] == qIndex ){  
                           return true;
                      }else{
                          return false;
                      }
                  }             
              }else{
                  return false;
              }     
        }
        
          $scope.evaluate_Report = function(isValid)
          {
              if(isValid){
                  alert('Form is valid');
              }else{
                  alert('Form is Incomplete');
              }
        }
         
    }]);

然后

    .module {
    	
    /*	box-shadow: 0 0 3px rgba(0,0,0,.1); */
    	border-color: #e9e9e9;
    	margin-bottom: 50px;
        background-color: #fff;
       /* border-radius: 4px;*/
    /*    -webkit-box-shadow: 0 1px 1px rgba(0,0,0,.05);*/
        box-shadow: 0 1px 1px rgba(0,0,0,.05);	   
    	-webkit-border-radius: 3px;
    	-moz-border-radius: 3px;
    	border-radius: 3px;
    	border: 1px solid #ccc;
    	border-bottom-color: #bbb;
    	-webkit-box-shadow: 0 0 1px rgba(0,0,0,0.2);
    	-moz-box-shadow: 0 0 1px rgba(0,0,0,0.2);
    	box-shadow: 0 0 1px rgba(0,0,0,0.2);
    }


    .module-head {	
    	color: #767676;
    	background-color: #f6f6f6;
    	border-color: #e9e9e9;
    	padding: 10px 15px;
    	border-bottom: 1px solid transparent;
    	border-top-right-radius: 3px;
    	border-top-left-radius: 3px;		
    }

    .module-head h3 {
    	font-size: 20px;
    	line-height: 20px;
    	height: 20px;
    	margin: 0;
    	font-weight: bold;
    }

    .module-body {
    	padding: 15px;
        width:1350px;
        margin-right: auto;
        margin-left: auto;
    }

并且您将替换所有引号

修改

Replace函数的第一个参数可以是正则表达式