프로그래밍/java

\n 및 \r , <br>

antilla 2011. 5. 25. 10:10


등록한 문제를 스크립트로 호출 시 textarea 안에 있는 \n 이랑 <br> 땜에 스크립트 에러

function selectItem(in_no, vc_question, vc_sentence, vc_example1, vc_example2, vc_example3, vc_example4, in_answer, vc_type, vc_img_logical, vc_img_physical) {
        $('input[name=in_no]').val(in_no);
        $('textarea[name=vc_question]').val(vc_question.replace('<br>', '\n'));
      $('textarea[name=vc_sentence]').val(vc_sentence.replace(/<br>/gi, '\n'));

        $('input[name=vc_example1]').val(vc_example1);
        $('input[name=vc_example2]').val(vc_example2);
        $('input[name=vc_example3]').val(vc_example3);
        $('input[name=vc_example4]').val(vc_example4);
       
        $('input[name=original_img_logical]').val(vc_img_logical);
        $('input[name=original_img_physical]').val(vc_img_physical);
        $('select[name=in_answer]').val(in_answer);
        $('select[name=vc_type]').val(vc_type);
        $('input[name=vc_img_logical]').val(vc_img_logical);
        $('input[name=vc_img_physical]').val(vc_img_physical);
  $('#btnWrite').hide();
  $('#btnDelete').show();
  $('#btnEdit').show();
  $('#btnCancel').show();

  if( vc_img_physical != "" && vc_img_physical != 'null'){

   var imgHtml = "";
   
   imgHtml += "<img src=\"/upload_file/vc/" + vc_img_physical + "\" width='100px' height='100px'>";
   imgHtml += "<input type='hidden' name='vc_img_logical' id='vc_img_logical' value='" + vc_img_logical + "'>";
   imgHtml += "<input type='hidden' name='vc_img_physical' id='vc_img_physical' value='" + vc_img_physical + "'>";
   imgHtml += "<input type='button' value='삭제' onclick='javascript:imgDel();'/>";
   
   document.getElementById("imgArea").innerHTML = imgHtml;
   
  } else {

   var imgHtml = "";
   
   imgHtml += "<input type='hidden' name='vc_img_logical' id='vc_img_logical'>";
   imgHtml += "<input type='hidden' name='vc_img_physical' id='vc_img_physical'>";
   imgHtml += "<input type='button' value='지문 이미지 등록' onclick='javascript:pop();'/>";
   
   document.getElementById("imgArea").innerHTML = imgHtml;
  }
 }



replace로 처리를 해주었더니 등록한 문제는 불러오나
리퀘스트로 불러오는 페이징처리부분에서 또 다시 스크립트 에러
그래서 jsp가 아닌 java에서 replace 처리


data.put("VC_QUESTION", StringUtil.setBR(StringUtil.evl( rs.getString("VC_QUESTION"), "" )) );

info.put("vc_question" , StringUtil.replace( StringUtil.replace( rs.getString("vc_question"),"\r\n", "<br>") ,"\"", "〃"));
info.put("vc_sentence" , StringUtil.replace( StringUtil.replace( rs.getString("vc_sentence"),"\r\n", "<br>") ,"\"", "〃"));

처리해주다 보니 " 쌍따옴표도 스크립트로 인식을 함
""" , "〃" 이런식으로 했으나 바꾸려는 문자열이"(쌍따옴표) 이다보니
문자열을 감싸는 " 인줄 알고 ); 가 닫히지 않아 컴파일 에러
그리하여 " 앞에 역슬래시 추가  ----> "\"", "〃"



*팁 : 정규식 \gi

g : 발생할 모든 pattern에 대한 전역 검색
i : 대/소문자 구분 안함
m : 여러줄 검색