admin管理员组文章数量:1434916
For my website I use the dataTable plugin and to give the user the possibility to filter the results. I implemented some filter, which are dynamically loaded from the results and contains the all different values of each column.
As more than one filter can be bined, I deactived the smart search and had to active the regEx-search instead. All these things are working fine.
My Problem is: I have content like "content (another content)", and for those contents containing brackets, the search doesn't work (no result is found).
Is there a possibility to mask the searchString
before calling:
table.column('myColumn:name').search(searchString, true, false, true).draw();
I tried to replace the string with "\)" or something like this, but that doesn't help. If I just delete those special characters the results can't be found either as regEx search needs exact strings.
Can anyone help me please?
For my website I use the dataTable plugin and to give the user the possibility to filter the results. I implemented some filter, which are dynamically loaded from the results and contains the all different values of each column.
As more than one filter can be bined, I deactived the smart search and had to active the regEx-search instead. All these things are working fine.
My Problem is: I have content like "content (another content)", and for those contents containing brackets, the search doesn't work (no result is found).
Is there a possibility to mask the searchString
before calling:
table.column('myColumn:name').search(searchString, true, false, true).draw();
I tried to replace the string with "\)" or something like this, but that doesn't help. If I just delete those special characters the results can't be found either as regEx search needs exact strings.
Can anyone help me please?
Share Improve this question edited Sep 30, 2015 at 13:21 Gyrocode. 59k16 gold badges157 silver badges192 bronze badges asked Sep 29, 2015 at 14:34 user2550641user2550641 3474 silver badges20 bronze badges1 Answer
Reset to default 6SOLUTION
You can add and use a function escapeRegExp()
that will escape special
characters as found in MDN - Regular Expressions article:
function escapeRegExp(string){
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
// ... skipped ...
table.column('myColumn:name').search(escapeRegExp(searchString), true, false, true).draw();
DEMO
See this jsFiddle for code and demonstration.
本文标签: javascriptHow to escape special characters in regular expressionsStack Overflow
版权声明:本文标题:javascript - How to escape special characters in regular expressions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745643866a2668019.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论