admin管理员组文章数量:1254251
I'm using elasticsearch in a NodeJS project, which is breaking upon entering one of the Lucene keywords into the search box. I'm looking for a simple implementation to escape their special characters. Is regex the best way? I've been fiddling with the regex for a bit now, and felt as if there were probably others that have already gone through this process.
I'm using elasticsearch in a NodeJS project, which is breaking upon entering one of the Lucene keywords into the search box. I'm looking for a simple implementation to escape their special characters. Is regex the best way? I've been fiddling with the regex for a bit now, and felt as if there were probably others that have already gone through this process.
Share Improve this question asked Oct 17, 2014 at 19:19 user2402831user2402831 67311 silver badges24 bronze badges2 Answers
Reset to default 13Just needed to fiddle more.
var escaped = query.replace(/([\!\*\+\&\|\(\)\[\]\{\}\^\~\?\:\"])/g, "\\$1");
Globally replace all instances of the chars lucene would yell about with their escaped version.
I updated this to include all elasticsearch & Lucene reserved characters:
var pattern = /([\!\*\+\-\=\<\>\&\|\(\)\[\]\{\}\^\~\?\:\\/"])/g;
var str = "}the+S+entence you want]]to e*scape"
str = str.replace(pattern, "\\$1");
本文标签: Escaping Lucene Characters using Javascript RegexStack Overflow
版权声明:本文标题:Escaping Lucene Characters using Javascript Regex - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740768949a2283183.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论