NestJs Sample RegExp

Yadollah khaje hosseini
3 min readMay 9, 2022

--

Search in full text

if (searchCityDto.hasOwnProperty('title')) {
Object.assign(filter, {
"title.fa": new RegExp(searchCityDto.title, 'i'),
});
cacheKey += '_'+ searchCityDto.title;
}

Search in start of word

if (searchCityDto.hasOwnProperty('title')) {
Object.assign(filter, {
"title.fa": new RegExp('^'+searchCityDto.title, 'i'),
});
cacheKey += '_'+ searchCityDto.title;
}

With redis

/**
* get all city for one country
*
@param searchStateDto
*/
async cities(searchCityDto: SearchCityDto) {
let filter = {};

Object.assign(filter, { 'topicType': LocationTopicTypeEnum.CITY});

let cacheKey = 'search_in_cities';

if (searchCityDto.hasOwnProperty('countryId')) {
Object.assign(filter, { 'countryId': searchCityDto.countryId});
cacheKey += '_'+ searchCityDto.countryId;
}

if (searchCityDto.hasOwnProperty('stateId')) {
Object.assign(filter, { 'stateId': searchCityDto.stateId});
cacheKey += '_'+ searchCityDto.stateId;
}

if (searchCityDto.hasOwnProperty('title')) {
Object.assign(filter, {
"title.fa": new RegExp('^'+searchCityDto.title, 'i'),
});
cacheKey += '_'+ searchCityDto.title;
}

/**
* get cache
*/
let getCache = await this.cacheManager.get(cacheKey);
if(!getCache){
/**
* find Db
*/
let data = await this.locationModel.find(filter);
/**
* set cache for 1 day = 86400 second
*/
await this.cacheManager.set(cacheKey,[...new Set(data)], {ttl:86400});

getCache = data;


}

return getCache;
}

4: search on fullName

if (searchUserDto.hasOwnProperty('fullName')) {
searchUserDto.fullName = this._strReplaceForArabic(searchUserDto.fullName);

let tempFullName = searchUserDto.fullName;
let fullNameWithOutSpace = tempFullName.replace( " ", '');

arrConditionAnd.push(

{
"$expr": {
"$or":[
{
"$regexMatch": {
"input": {"$concat": ["$firstName", " ", "$lastName"]},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": "$firstName",
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": "$lastName",
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$replaceAll": { input: "$firstName", find: " ", replacement: ""}},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$replaceAll": { input: "$firstName", find: "‌", replacement: ""}},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$replaceAll": { input: "$lastName", find: " ", replacement: ""}},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$replaceAll": { input: "$lastName", find: "‌", replacement: ""}},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": "$firstName",
"regex": fullNameWithOutSpace,
"options": "i"
}
},
{
"$regexMatch": {
"input": "$lastName",
"regex": fullNameWithOutSpace,
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": ["$firstName", " ", "$lastName"]},
"regex": fullNameWithOutSpace,
"options": "i"
}
},
]

}
}
);
}

5: search on fullName (New)

let tempFullName         =  searchUserDto.fullName;
let fullNameWithOutSpace = tempFullName.replace( new RegExp(" ", 'g'), '')
.replace( new RegExp("‌", 'g'), '');

arrConditionAnd.push(

{
"$expr": {
"$or":[
{
"$regexMatch": {
"input": {"$concat": ["$firstName", " ", "$lastName"]},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": [{"$replaceAll": { input: "$firstName", find: " ", replacement: ""}}, " ", "$lastName"]},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": [{"$replaceAll": { input: "$firstName", find: "‌", replacement: ""}}, " ", "$lastName"]},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": ["$firstName", " ", {"$replaceAll": { input: "$lastName", find: " ", replacement: ""}}]},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": ["$firstName", " ", {"$replaceAll": { input: "$lastName", find: "‌", replacement: ""}}]},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": [{"$replaceAll": { input: "$firstName", find: " ", replacement: ""}}, " ", {"$replaceAll": { input: "$lastName", find: " ", replacement: ""}}]},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": [{"$replaceAll": { input: "$firstName", find: "‌", replacement: ""}}, " ", {"$replaceAll": { input: "$lastName", find: "‌", replacement: ""}}]},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": "$firstName",
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": "$lastName",
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$replaceAll": { input: "$firstName", find: " ", replacement: ""}},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$replaceAll": { input: "$firstName", find: "‌", replacement: ""}},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$replaceAll": { input: "$lastName", find: " ", replacement: ""}},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$replaceAll": { input: "$lastName", find: "‌", replacement: ""}},
"regex": searchUserDto.fullName.toString(),
"options": "i"
}
},
{
"$regexMatch": {
"input": "$firstName",
"regex": fullNameWithOutSpace,
"options": "i"
}
},
{
"$regexMatch": {
"input": "$lastName",
"regex": fullNameWithOutSpace,
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": ["$firstName", " ", "$lastName"]},
"regex": fullNameWithOutSpace,
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": [{"$replaceAll": { input: "$firstName", find: "‌", replacement: ""}}, {"$replaceAll": { input: "$lastName", find: "‌", replacement: ""}}]},
"regex": fullNameWithOutSpace,
"options": "i"
}
},
{
"$regexMatch": {
"input": {"$concat": [{"$replaceAll": { input: "$firstName", find: " ", replacement: ""}}, {"$replaceAll": { input: "$lastName", find: " ", replacement: ""}}]},
"regex": fullNameWithOutSpace,
"options": "i"
}
},
]

}
}
);

}

--

--

Yadollah khaje hosseini
Yadollah khaje hosseini

No responses yet