码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-04-29 21:29:31      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

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>

 

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

标签:

原文地址:http://www.cnblogs.com/a198720/p/4466826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!