js:正则表达式在Knockout条件注释中查找字符串

时间:2016-03-21 06:33:05

标签: javascript regex knockout.js

我必须使用Regex在Knockout条件注释中找到一个字符串。场景是,将有两种 KnockoutConditionalStatement ,这将根据用户详细信息付费/未付款而有所不同。

陈述 - 我 - 未付款

<!-- ko if: IsPaidUser() --><!-- /ko -->
<!-- ko if: !IsPaidUser() -->
<!-- ko if: $parent.isEditable($data.UserName, $data.UserRole) -->
<i title="NotPaid" class="fa fa-toggle-on faIcons fa-rotate-180 toggleOff" data bind="click: function (data, event) { $parent.toggleUserAccountStatus($index(), 'reactivate') }"></i>
<!-- /ko -->
<!-- ko if: !$parent.isEditable($data.UserName, $data.UserRole) --><!-- /ko -->
<i style="display: none;">00</i>
<!-- /ko -->

陈述 - II - 付费

<!-- ko if: IsPaidUser() -->
<!-- ko if: $parent.isEditable($data.UserName, $data.UserRole) -->
<i title="Paid" class="fa fa-toggle-on faIcons green" data-bind="click: function (data, event) { $parent.toggleUserAccountStatus($index(), 'deactivate') }"></i>
<!-- /ko -->
<!-- ko if: !$parent.isEditable($data.UserName, $data.UserRole) --><!-- /ko -->
<i style="display: none;">01</i>
<!-- /ko -->
<!-- ko if: !IsPaidUser() --><!-- /ko -->

根据用户详细信息,Statement-I或Statement-II将保存在状态

var status = knockoutConditionalStatement;

任务是,

  1. 必须检查用户是付费客户还是未付费客户
  2. 必须检查状态是否为#34; NotPaid&#34;或&#34;付费&#34; knockoutConditionalStatement
  3. 如果单词&#34;未付款&#34;在knockoutConditionalStatement中找到意味着必须将许可证设置为false
  4. if word&#34;付费&#34;发现knockoutConditionalStatement表示必须将License设置为true
  5. 我尝试过类似方法-I,

    var status = knockoutConditionalStatement;
    var license = false;
    if (string.search("title=\"Paid\""))
    {
        license = true;
    }
    
    else
    {
        license = false;
    }
    // this code run without any error. but license mode did not analysed.
    

    我也尝试过这种方法-II,(可能是愚蠢的)

    if(String == '<!-- ko if: IsPaidUser() --><!-- /ko --><!-- ko if: !IsPaidUser() --><!-- ko if: $parent.isEditable($data.UserName,$data.UserRole) --><i title="NotPaid" class="fa fa-toggle-on faIcons fa-rotate-180toggleOff" data-bind="click: function (data, event) { $parent.toggleUserAccountStatus($index(), 'reactivate')}"></i><!-- /ko --><!-- ko if: !$parent.isEditable($data.UserName, $data.UserRole) --><!-- /ko --><i style="display: none;">00</i><!-- /ko -->')
    {
        license = true;
    }
    
    else
    {
        license = false;
    }
    // this code always getting into else{} block
    

    请建议我如何解析淘汰条件声明并预测许可证详细信息。

    先谢谢

    编辑-1:工作代码:

    var status = knockoutConditionalStatement;
    var license = false;
        if (string.search("title=\"Paid\"") != -1 ) // if found
        {
            license = true;
        }
    
        else // not found
        {
            license = false;
        }
    

0 个答案:

没有答案