js中直接对字符串转义-用于solr ulr 关键词转义

js代码

  

/*
 * 获取UTC格式的字符串,参数必须是
 */
var solrDateFormat = function (o){
    var date;
    if(typeof o == ‘string‘){
        date = new Date(o);
    }else if(o instanceof Date){
        date = o;
    }else{
        return ‘‘;
    }
    return date.getUTCFullYear() + ‘-‘ + (date.getUTCMonth() + 1) + ‘-‘
    + date.getUTCDate() + ‘T‘ + date.getUTCHours() + ‘:‘
    + date.getUTCMinutes() + ‘:‘ + date.getUTCSeconds() + ‘Z‘;
};

/*
* 转义字符查询 */ var escapeQueryChars = function (q){ var result = ‘‘; //如果字符串q 不为空(null,未定义,空) if (q !== null && q !== undefined && q !== ‘‘ && q !=="") { for(var i=0;i < q.length;i++){ var c = q.charAt(i); if (c == ‘\\‘ || c == ‘+‘ || c == ‘-‘ || c == ‘!‘ || c == ‘(‘ || c == ‘)‘ || c == ‘:‘ || c == ‘^‘ || c == ‘[‘ || c == ‘]‘ || c == ‘\"‘ || c == ‘{‘ || c == ‘}‘ || c == ‘~‘ || c == ‘*‘ || c == ‘?‘ || c == ‘|‘ || c == ‘&‘ || c == ‘;‘ || c == ‘/‘ || c == ‘ ‘) { result = result+ ‘\\‘; } result = result+ c; } }else{ return "*:*"; } return result; };

测试:

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="json3/produce/json3.min.js"></script>
<script type="text/javascript" src="js/myUtil.js"></script>
<script type="text/javascript">
    //var date = new Date("2013/9/29 11:10:26");
    //console.info(solrDateFormat(date));
    //console.info(solrDateFormat("2013/9/29 11:10:26"));
    //console.info(solrDateFormat("2013/09/29 21:10:26"));
    
    // 测试 escapeQueryChars方法
    var  q = "天翼大众 (1)";
    alert(escapeQueryChars(q));
</script>
<title>搜索测试</title>
</head>
<body>
    <div>
        <input id="keyword" type="text"> <input id="search" type="button"
            value="search" >
    </div>
    <div id="result">
    </div>
</body>
</html>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。