如何用新的RegExp()构建一个正则表达式

时间:2016-11-21 13:31:15

标签: javascript regex

拥有一组看起来像这样的本地存储密钥

[
  '$gl-user',
  '$gl-date-preference::22'
  '$gl-date-preference::28'
  '$gl-mg-filters::22::1'
  '$gl-mg-filters::22::8'
]
  • 首先::_number_代表storeId
  • 第二个::_number_可以 表示任何其他标识符。

尝试构建一个带storeId的函数,并返回与storeId匹配的所有键。因此,如果将22传递给该函数,它将返回

[
  '$gl-date-preference::22',
  '$gl-mg-filters::22::1',
  '$gl-mg-filters::22::8'
]

这是我的第一次尝试。将其复制到控制台每次都返回null但我不明白为什么。

var regex = new RegExp('^[$\w\d\-]+\:\:' + '22');
'$gl-mg-filters::22'.match(regex);

任何有关使这个正则表达式工作的帮助,或者对更好的解决方案的想法都将非常感激。谢谢!

1 个答案:

答案 0 :(得分:0)

你的正则表达式不匹配,因为你只是用字符串里面的斜杠转义。相反,你应该逃避它两次,例如:

            <lookup filterClass="application.handlers.WODetailHandler" filterMethod="filterWorktypeForLookup" id="WorkExecution.WorkTypeLookup" label="Select Work Type" resource="additionalworktype">
        <requiredResources id="WorkExecution.WorkTypeLookup_requiredResources">
            <requiredResource id="WorkExecution.WorkTypeLookup_additionalworktype" name="additionalworktype"/>
            <requiredResource id="WorkExecution.WorkTypeLookup_domainwoclass" name="domainwoclass"/>
        </requiredResources>
        <list id="WorkExecution.WorkTypeLookup_additionalworktype_list" resource="additionalworktype">
            <searchAttributes id="WorkExecution.WorkTypeLookup_additionalworktype_searchAttributes">
                <searchAttribute id="WorkExecution.WorkTypeLookup_additionalworktype_searchAttribute_worktype" name="worktype"/>
                <searchAttribute id="WorkExecution.WorkTypeLookup_additionalworktype_searchAttribute_wtypedesc" name="wtypedesc"/>
                <searchAttribute id="WorkExecution.WorkTypeLookup_additionalworktype_searchAttribute_brdbwtcategory" name="brdbwtcategory"/>
            </searchAttributes>
            <listItemTemplate id="WorkExecution.WorkTypeLookup_additionalworktype_listItemTemplate_Item1Desc1" layout="Item1Desc1">
                <listtext cssClass="bold textappearance-medium" id="WorkExecution.WorkTypeLookup_additionalworktype_Item1Desc1_worktype" layoutInsertAt="item1" resourceAttribute="worktype"/>
                <listtext cssClass="bold textappearance-medium" id="WorkExecution.WorkTypeLookup_additionalworktype_Item1Desc1_wtypedesc" layoutInsertAt="item2" resourceAttribute="wtypedesc"/>
                <listtext cssClass="bold textappearance-medium" id="WorkExecution.WorkTypeLookup__additionalworktype_Item1Desc1_brdbwtcategory" layoutInsertAt="item3" resourceAttribute="brdbwtcategory"/>
            </listItemTemplate>
        </list>
    </lookup>

您的初始尝试会尝试将 {"spi:promptdown": false, "spi:contentuid": "9", "spi:promptfail": false, "spi:worktype": "FO", "dcterms:title": "Follow On", "spi:brdbwtcategory": "CM", "rdf:type": [ { "rdf:resource": "http://jazz.net/ns/ism/asset/smarter_physical_infrastructure#WorkType" } ], "spi:orgid": "TESTORG", "spi:woclass_description": "Work Order", "rdf:about": "http://xxxxx.xxx-xxx.xxx:32800/maximo/oslc/os/oslcworktype/_OQ--", "spi:keeptaskstatushist": true, "dcterms:identifier": "9", "prefixes": { "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "dcterms": "http://purl.org/dc/terms/", "spi": "http://jazz.net/ns/ism/asset/smarter_physical_infrastructure#", "oslc": "http://open-services.net/ns/core#" }, "_rowstamp": "1699854", "spi:woclass": "WORKORDER", "spi:brdbwtcategory_description": "Corrective Maintenance", "spi:type_description": "Not Applicable", "spi:type": "NONE" } 编译为正则表达式。