在控制器中,使用findAll(),我試圖創建一個 where 子句來回傳所有命中,而不考慮搜索字串中的標點符號。目前,即使我使用的是[Op.iLike]運算子,標點符號也會指示回傳的內容。
例子:
search可以是'St Andrew'或'St. Andrew'
在以下示例中: ifsearch === 'St. Andrew'只回傳帶有St. Andrew
And 的結果,如果search === 'St Andrew'它只回傳帶有 And 的結果St Andrew
where: { name: { [Op.iLike]: `%${search}%` } },
在以下示例中: ifsearch === 'St. Andrew'回傳所有預期結果(帶有或不帶 的結果.) 但是,如果search === 'St Andrew',它只回傳不帶.
const noPunctuationSearch = search?.replace(/[^A-Za-z0-9] /g, ' ');
where: {
name: {
[Op.or]: [
{ [Op.iLike]: `%${search}%` },
{ [Op.iLike]: `%${noPunctuationSearch}%` },
],
},
},
如何設定 where 子句和運算子以回傳所有St. Andrew || St Andrew結果,而不管 中的標點符號search如何?
uj5u.com熱心網友回復:
您可以使用Op.iRegexp,但您需要對查詢進行更多處理以從中創建正則運算式。
const search = 'St. Andrews';
const noPuncuationSearch = search?.replace(/[^A-Za-z0-9] /g,'[^A-Za-z0-9] ');
where: {
name: {
[Op.iRegexp]: noPuncuationSearch,
},
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/436665.html
標籤:节点.js PostgreSQL 续集.js
