簡體   English   中英

我想操縱正則表達式返回的捕獲數據

[英]I would like to manipulate captured data from a regular expression return

我正在編寫一個命令來搜索我的html文檔,並根據文本節點向任何標題標記添加一個id。 我想知道是否可以捕獲文本節點,然后用連字符替換空格。

例如

<h2>This is a heading</h2>

會成為

<h2 id="this-is-a-heading">This is a heading</h2>

我是正則表達的新手,請原諒我,如果這是一個愚蠢的問題。

目前我有這個,但意識到它不會改變捕獲的數據。

搜索條件:

<h2\s*>([^<]*)</h2>

替換文字:

<h2 id="$1">$1</h2>

提前致謝。

你可能真的不需要正則表達式,但你可以做到這一點。

普通的JavaScript示例

 window.onload = function() { var element = document.getElementById("somenode"); var text = element.innerHTML; element.innerHTML = text.replace(" ", "-"); }; 
 <h1 id="somenode">Hello world!</h1> 

我現在不得不分階段做我想做的事情。

這就是我到目前為止所做的。

改變

<h2>1. Something Here</h2>

<h2 id="Something-here">1. Something Here</h2>

要查找除h1之外的所有標題標簽...

        dw.setUpFindReplace({searchString: '<h([^1])>([^<]*)', 
    replaceString: '<h$1 id="$2">$2', searchSource: true, 
matchCase: false, ignoreWhitespace: false, 
useRegularExpressions: true, searchWhat: 'document'});
                dw.replaceAll();

刪除id開頭的任何數字...

        dw.setUpFindReplace({searchString: '<h([^1]) id="\s*(\d+\s+|\d+,\d+\s+|\d+\.)', 
replaceString: '<h$1 id="', searchSource: true, matchCase: false, 
ignoreWhitespace: false, useRegularExpressions: true, 
searchWhat: 'document'});
            dw.replaceAll();

連字符號...

     dw.setUpFindReplace({searchString: '<h([^1]) id="(\w+)\s+(\w+)\s*', 
replaceString: '<h$1 id="$2-$3', 
    searchSource: true, matchCase: false, 
    ignoreWhitespace: false, useRegularExpressions: true, 
    searchWhat: 'document'});
                        dw.replaceAll();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM