如何使用knexjs在postgresql中搜索具有特殊字符和其他字符的文本?

How to search text having special character with other characters in postgresql using knexjs?

我有一个 table 由 hotels.One 组成的酒店名称是 d'spa.I 如果用户搜索 d'spa,他已经在 names.So 上实现了搜索,他会得到合适的酒店。

现在,如果用户搜索 d.spa、d-spa 或 dspa,用户应该找到正确的酒店,即 d'spa

我的 knexjs 查询(没有特殊字符问题):

hotel = "d'spa"
knex.select('*').from('hotels').where({name:hotel})

我想要与

相同的结果
hotel = "d-spa" 
//or hotel = "d.spa" 
//or hotel = "dspa"

您可以在 SQL 请求中使用正则表达式匹配,并将酒店名称中的所有非字母数字字符替换为 .?

? denotes repetition of the previous item zero or one time.

例如:

const hotelMatch = hotel.replace(/[\W]+/g, '.?')

然后执行 regex request 请求:

knex.select('*').from('hotels').where(knex.raw(`name SIMILAR TO ?`, [hotelMatch]))

/!\我没有测试代码,它作为示例 /!\