xzwilliam
xzwilliam
采纳率0%
2016-05-15 08:37

JS正则表达式替换页面中内容

 我想用正则表达式替换table中除了标签"<tr class="template" ng-repeat="model in model.child"></td>"里面的ng-model中的内容,将ng-model="model.*"替换成ng-model="model.xx.*"。
html页面如下:
        <table width="592" height="842" border="1" align="center">
          <tr>
            <td align="center" colspan="2" width="16%">服务资源名称</td>
            <td colspan="8"><input class="text" name="serve_resource_name" type="text" ng-model="model.serve_resource_name" id="serve_resource_name" title="服务资源名称" value="" maxlength="" plugins="text" fallowblank="false" datatype="text"/></td>
          </tr>
          <tr>
            <td align="center" rowspan="4" width="8%">访<br>问<br>端</td>
            <td align="center" style="width:8%">系统</td>
            <td colspan="3"><input class="text" name="system" type="text" ng-model="model.system" id="system" title="系统" value="" maxlength="" plugins="text" fallowblank="false" datatype="text"/></td>
            <td align="center" colspan="2" width="16%">IP</td>
            <td colspan="3"><input class="text" name="ip" type="text" ng-model="model.ip" id="ip" title="IP" value="" maxlength="" plugins="text" fallowblank="false" datatype="text"/></td>
          </tr>
          <tr>
            <td align="center">用户名</td>
            <td colspan="3"><input class="text" name="username" type="text" ng-model="model.username" id="username" title="用户名" value="" maxlength="" plugins="text" fallowblank="false" datatype="text"/></td>
            <td align="center" colspan="2" width="16%">密码</td>
            <td colspan="3"><input class="text" name="password" type="text" ng-model="model.password" id="password" title="密码" value="" maxlength="" plugins="text" fallowblank="false" datatype="text"/></td>
          </tr>
          <tr>
            <td align="center" width="16%">期限</td>
            <td colspan="8"><input class="text" name="time_limit" type="text" ng-model="model.time_limit" id="time_limit" title="期限" value="" maxlength="" plugins="text" fallowblank="false" datatype="text"/></td>
          </tr>

           <tr>
                <td align="center" width="8%" height="150px">流<br>量<br>控<br>制</td>
                <td colspan="9" style="padding:0; vertical-align:top;">
                <div style="overflow:auto; height:150px;">
                    <table id="697e5afad8e540659efb412ea0347b5d" name="llkz" fnote="流量控制" fdatatype="1" plugins="childtable" border="1" cellspacing="0" cellpadding="0">
                        <thead>
                            <tr class="firstRow">
                                <td align="center" width="24%">时间段</td>
                                <td align="center" width="24%">时间间隔<br>(次/分钟)</td>
                                <td align="center" width="24%">访问次数上限</td>
                                <td align="center" width="24%">访问流量上限</td>
                                <td align="center"  ><div class="img-add" ng-click="imgAdd('model.table')"></div></td>
                          </tr>
                        </thead>
                        <tbody>
                            <tr class="template" ng-repeat="model in model.child">
                                <td ><input name="time_start" type="text" ng-model="model.time_start" id="time_start" title="开始时间" value="" maxlength="" placeholder="8:00"/>至<input name="time_end" type="text" ng-model="model.time_end" id="time_end" title="结束时间" value=""placeholder="22:00"/></td>
                                <td ><input name="time_interval_count" type="text" ng-model="model.time_interval_count" id="time_interval_count" title="时间间隔次数" value="" />/<input name="time_interval_min" type="text" ng-model="model.time_interval_min" id="time_interval_min" title="时间间隔分钟数" value="" /></td>
                                <td ><input class="text" name="visit_count_limit" type="text" ng-model="model.visit_count_limit" id="visit_count_limit" title="访问次数上限" value="" "/></td>
                                <td ><input class="text" name="visit_flux_limit" type="text" ng-model="model.visit_flux_limit" id="visit_flux_limit" title="访问流量上限" value="" /></td>
                            </tr>
                        </tbody>

                    </table></div>
                </td>
            </tr>
          <tr>
            <td align="center" width="8%">备<br>注</td>
            <td colspan="9">
             <div class="word-text"><textarea class="bor" id="remark" name="remark" ng-model="model.remark" title="申请理由及目的" maxlength="" plugins="textarea" fallowblank="false"></textarea></div>
            </td>
          </tr>
          </table>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • showbo GoCityPass新加坡曼谷通票 5年前
     <textarea id="ta">
        <table width="592" height="842" border="1" align="center">
            <tr>
                <td align="center" colspan="2" width="16%">服务资源名称</td>
                <td colspan="8"><input class="text" name="serve_resource_name" type="text" ng-model="model.serve_resource_name" id="serve_resource_name" title="服务资源名称" value="" maxlength="" plugins="text" fallowblank="false" datatype="text" /></td>
            </tr>
            <tr>
                <td align="center" rowspan="4" width="8%">访<br>问<br>端</td>
                <td align="center" style="width:8%">系统</td>
                <td colspan="3"><input class="text" name="system" type="text" ng-model="model.system" id="system" title="系统" value="" maxlength="" plugins="text" fallowblank="false" datatype="text" /></td>
                <td align="center" colspan="2" width="16%">IP</td>
                <td colspan="3"><input class="text" name="ip" type="text" ng-model="model.ip" id="ip" title="IP" value="" maxlength="" plugins="text" fallowblank="false" datatype="text" /></td>
            </tr>
            <tr>
                <td align="center">用户名</td>
                <td colspan="3"><input class="text" name="username" type="text" ng-model="model.username" id="username" title="用户名" value="" maxlength="" plugins="text" fallowblank="false" datatype="text" /></td>
                <td align="center" colspan="2" width="16%">密码</td>
                <td colspan="3"><input class="text" name="password" type="text" ng-model="model.password" id="password" title="密码" value="" maxlength="" plugins="text" fallowblank="false" datatype="text" /></td>
            </tr>
            <tr>
                <td align="center" width="16%">期限</td>
                <td colspan="8"><input class="text" name="time_limit" type="text" ng-model="model.time_limit" id="time_limit" title="期限" value="" maxlength="" plugins="text" fallowblank="false" datatype="text" /></td>
            </tr>
    
            <tr>
                <td align="center" width="8%" height="150px">流<br>量<br>控<br>制</td>
                <td colspan="9" style="padding:0; vertical-align:top;">
                    <div style="overflow:auto; height:150px;">
                        <table id="697e5afad8e540659efb412ea0347b5d" name="llkz" fnote="流量控制" fdatatype="1" plugins="childtable" border="1" cellspacing="0" cellpadding="0">
                            <thead>
                                <tr class="firstRow">
                                    <td align="center" width="24%">时间段</td>
                                    <td align="center" width="24%">时间间隔<br>(次/分钟)</td>
                                    <td align="center" width="24%">访问次数上限</td>
                                    <td align="center" width="24%">访问流量上限</td>
                                    <td align="center"><div class="img-add" ng-click="imgAdd('model.table')"></div></td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr class="template" ng-repeat="model in model.child">
                                    <td><input name="time_start" type="text" ng-model="model.time_start" id="time_start" title="开始时间" value="" maxlength="" placeholder="8:00" />至<input name="time_end" type="text" ng-model="model.time_end" id="time_end" title="结束时间" value="" placeholder="22:00" /></td>
                                    <td><input name="time_interval_count" type="text" ng-model="model.time_interval_count" id="time_interval_count" title="时间间隔次数" value="" />/<input name="time_interval_min" type="text" ng-model="model.time_interval_min" id="time_interval_min" title="时间间隔分钟数" value="" /></td>
                                    <td><input class="text" name="visit_count_limit" type="text" ng-model="model.visit_count_limit" id="visit_count_limit" title="访问次数上限" value="" " /></td>
                                    <td><input class="text" name="visit_flux_limit" type="text" ng-model="model.visit_flux_limit" id="visit_flux_limit" title="访问流量上限" value="" /></td>
                                </tr>
                            </tbody>
    
                        </table>
                    </div>
                </td>
            </tr>
            <tr>
                <td align="center" width="8%">备<br>注</td>
                <td colspan="9">
                    <div class="word-text"><textarea class="bor" id="remark" name="remark" ng-model="model.remark" title="申请理由及目的" maxlength="" plugins="textarea" fallowblank="false"></textarea></div>
                </td>
            </tr>
        </table>
    </textarea>
    <script>
        var ta = document.getElementById('ta'), s = ta.value,template='';
        s = s.replace(/<tr\s+class="template"[\s\S]+?<\/tr>/, function ($0) { template = $0; return '{template}' })
            .replace(/ng-model="([^"]+)"/g, function ($0,$1) {return 'ng-model="'+$1.replace('.','.xx.')+'"' })
            .replace('{template}', template)
        ta.value = s;
    </script>
    
    点赞 评论 复制链接分享
  • havedream_one havedream_one 5年前

    查找 (.*)ng-model="model.(.*)(".*)

    替换 \1ng-model="model.xx.\2\3

    点赞 评论 复制链接分享
  • showbo GoCityPass新加坡曼谷通票 5年前

    dom操作就行了,正则不好弄

    
    你的html代码
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
    <script>
        $('[ng-model]').attr('ng-model', function () {
            if ($(this).closest('tr.template').length == 0) return this.getAttribute('ng-model').replace('.', '.xx.');
            return this.getAttribute('ng-model');
        });
    </script>
    
    点赞 评论 复制链接分享