本文介绍了如何使用$ regex搜索数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有数组SearchserviceStreet的文档,我们想用正则表达式搜索它。我尝试了不同的变体,但无法成功搜索。

We have document with array SearchserviceStreet, we want to use regex to search in it. I tried different variation but wasn't able to successfully search through it.

你能帮我吗?

Thx

{

" _id" :70481,

" UtilityID" :1021,

" CustomerID" :70481,

" AccountNumber" :""200026846840-0400134732",

" OccupantCode" :null,

" ClientEntityLookupDisplayName" :"商业",

" BusinessNumber" :" 400134732",

" MeterNumber" :" 2",

" Name" :"000000000 Ontario Inc",

" ExternalLocationID" :" 700736718",

" BusinessOperatingName" :null,

" IsActive" :true,

" RateCode" :"Urban Gen Svc Energy 2TIER RPP",

" TIMEFRAME" :"",

" Ranking" :"",

" Tenancy" :"所有者",

"NAICS" :null,

" NAICSDescription" :null,

" SIC" :null,

" SICDescription" :null,

" serviceStreet" :"1111 SANDHILL DR",

" ; ServiceCity" :"BBBBBBB",

" ServicePostCode" :"M1M8M8",

" ServiceProvince" :"ON",

"mailingStreet"" :"1111 SANDHILL DR",

" mailingCity" :"BBBBBB",

" mailingPostCode" :"M1M8M8",

" mailingProvince" :"ON",

" BusinessPhone" :"1111111111        ",

" BusinessExtension" :null,

" FaxNumber" :"",

&HeatType" :"",

" WaterHeatType" :"",

" BuildingType" :"",

" kWH" :null,

"Kw" :null,

" Kva" :null,

" Pf" :null,

" BuildingYear" :null,

" IsFlaggedForMailing" :false,

" SA_MA" :null,

" CommunicationGroup" :null,

" SalesPerson" :"",

" HasSignedRetailerContract" :false,

" RetailerName" :null,

" RetailerContractSignedDate" :null,

" PrimaryContactName" :"Zelko",

" PrimaryTitle" ; :"Mr.",

" PrimaryPhone" :"1111111111    ",

" Email" :null,

" lastModified" :{

" $ date" :1529803322690

},

" MACodeId" :0,

" MaxDemand" :null,

"AverageDemand" :null,

" MaxMonthlyUsage" :null,

" AverageMonthlyUsage" :null,

" IsActiveFixed" :true,

" MoveInDate" :null,

" MeterInstallDate" :null,

" EmployeeNumber" :null,

"YearEstablished"" :"",

" URL" :null,

" OperationSize" :null,

" MailingCustomerName" :null,

" SubSegment" :null,

" PrimaryJobFunction" :"管理",

" CustomerIdString" :"      70481",

" ThirdSegment" :null,

" YearlyAverageBill" :null,

" SecondaryContactName" :"",

" SecondaryContactTitle" :"",

" SecondaryContactPhone" :"",

" SecondaryContactEmail" :"",

" SecondaryJobFunction" :"",

" ThirdContactName" :"",

" ThirdContactTitle" :"",

" ThirdContactPhone" :"",

" ThirdContactEmail" :"",

" ThirdJobFunction" :"",

" EnergyUsePerSqFt" :0,

" CallCenterFunction" :6,

" ProjectCommunicationCount" :0,

" StatusName_SBLCFF" :"待联系",

" OFANumber" :null,

" CustomerImportTypeId" :1,

" CustomerEmail" :"",

"ResidentPhone" :"1111111111        ",

" SecondaryPhone" :"",

" TotalRecords" :0,

" BRIStatus" :"Dismissed",

" SearchserviceStreet" :[

" 11111",

" sandhill",

" dr",

""

],

" SearchServiceCity":" 11111111",

" SearchServicePostCode":111111",

" SearchRateCode":" urban gen svc energy 2tier rpp",

" SearchMeterNumber":" 2",

" SearchBusinessOperatingName":"",

" SearchmailingCity":" ancaster",

" SearchBusinessPhone":" 1111111",

" SearchPrimaryPhone":" 111111111",

" SearchName":" 000000000 ontario inc",

" SearchSplitName":[

" 000000000",

" ontario",

" inc"

],

" SearchSubSegment":null

}

{
"_id" : 70481,
"UtilityID" : 1021,
"CustomerID" : 70481,
"AccountNumber" : "200026846840-0400134732",
"OccupantCode" : null,
"ClientEntityLookupDisplayName" : "Business",
"BusinessNumber" : "400134732",
"MeterNumber" : "2",
"Name" : "000000000 Ontario Inc",
"ExternalLocationID" : "700736718",
"BusinessOperatingName" : null,
"IsActive" : true,
"RateCode" : "Urban Gen Svc Energy 2TIER RPP",
"TimeFrame" : "",
"Ranking" : "",
"Tenancy" : "Owner",
"NAICS" : null,
"NAICSDescription" : null,
"SIC" : null,
"SICDescription" : null,
"serviceStreet" : "1111 SANDHILL DR ",
"ServiceCity" : "BBBBBBB",
"ServicePostCode" : "M1M8M8",
"ServiceProvince" : "ON",
"mailingStreet" : "1111 SANDHILL DR",
"mailingCity" : "BBBBBB",
"mailingPostCode" : "M1M8M8",
"mailingProvince" : "ON",
"BusinessPhone" : "1111111111        ",
"BusinessExtension" : null,
"FaxNumber" : "",
"HeatType" : "",
"WaterHeatType" : "",
"BuildingType" : "",
"kWH" : null,
"Kw" : null,
"Kva" : null,
"Pf" : null,
"BuildingYear" : null,
"IsFlaggedForMailing" : false,
"SA_MA" : null,
"CommunicationGroup" : null,
"SalesPerson" : "",
"HasSignedRetailerContract" : false,
"RetailerName" : null,
"RetailerContractSignedDate" : null,
"PrimaryContactName" : "Zelko ",
"PrimaryTitle" : "Mr.",
"PrimaryPhone" : "1111111111    ",
"Email" : null,
"lastModified" : {
"$date" : 1529803322690
},
"MACodeId" : 0,
"MaxDemand" : null,
"AverageDemand" : null,
"MaxMonthlyUsage" : null,
"AverageMonthlyUsage" : null,
"IsActiveFixed" : true,
"MoveInDate" : null,
"MeterInstallDate" : null,
"EmployeeNumber" : null,
"YearEstablished" : "",
"URL" : null,
"OperationSize" : null,
"MailingCustomerName" : null,
"SubSegment" : null,
"PrimaryJobFunction" : "Management",
"CustomerIdString" : "     70481",
"ThirdSegment" : null,
"YearlyAverageBill" : null,
"SecondaryContactName" : "",
"SecondaryContactTitle" : "",
"SecondaryContactPhone" : "",
"SecondaryContactEmail" : "",
"SecondaryJobFunction" : "",
"ThirdContactName" : "",
"ThirdContactTitle" : "",
"ThirdContactPhone" : "",
"ThirdContactEmail" : "",
"ThirdJobFunction" : "",
"EnergyUsePerSqFt" : 0,
"CallCenterFunction" : 6,
"ProjectCommunicationCount" : 0,
"StatusName_SBLCFF" : "To Be Contacted",
"OFANumber" : null,
"CustomerImportTypeId" : 1,
"CustomerEmail" : "",
"ResidentPhone" : "1111111111        ",
"SecondaryPhone" : "",
"TotalRecords" : 0,
"BRIStatus" : "Dismissed",
"SearchserviceStreet" : [
"11111",
"sandhill",
"dr",
""
],
"SearchServiceCity" : "11111111",
"SearchServicePostCode" : 111111",
"SearchRateCode" : "urban gen svc energy 2tier rpp",
"SearchMeterNumber" : "2",
"SearchBusinessOperatingName" : "",
"SearchmailingCity" : "ancaster",
"SearchBusinessPhone" : "1111111",
"SearchPrimaryPhone" : "111111111",
"SearchName" : "000000000 ontario inc",
"SearchSplitName" : [
"000000000",
"ontario",
"inc"
],
"SearchSubSegment" : null
}

推荐答案

你可以试试下面的代码片段:

Could you please try below code snippet:

var thisExpressions = [/something/, /something_else/, /and_something_else/];
var thisExpressions2 = [/else/, /something_else/, /and_something_else/];
var thisString = 'else';

function matchInArray(string, expressions) {

    var len = expressions.length,
        i = 0;

    for (; i < len; i++) {
        if (string.match(expressions[i])) {
            return true;
        }
    }

    return false;

};

setTimeout(function() {
    console.log(matchInArray(thisString, thisExpressions));
    console.log(matchInArray(thisString, thisExpressions2));
}, 200)​

参考  StackOverFlow Thread for the same.

Reference StackOverFlow Thread for the same.

Hope this will help resolve your problem.

Hope this will help resolve your problem.


这篇关于如何使用$ regex搜索数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 16:13