私はstackoverflowで多くのことを検索し、次のような非常に興味深いものを見つけました:
そして 、テキスト div と < を置き換える JavaScript 正規表現。>
しかし、div を data-type 属性に置き換え、文字列から data-type 属性を削除するという目標を実際に解析できなかったことがわかりました。
これが私がした方法です。
//Doesn't work with multi lines, just get first occurrency and nothing more.
// Regex: /\s?data\-type\=(?:['"])?(\d+)(?:['"])?/
var source_code = $("body").html();
var rdiv = /div/gm; // remove divs
var mxml = source_code.match(/\S?data\-type\=(?:['"])?(\w+)(?:['"])?/);
var rattr =source_code.match(/\S?data\-type\=(?:['"])?(\w+)(?:['"])/gm);
var outra = source_code.replace(rdiv,'s:'+mxml[1]);
var nestr = outra.replace(rattr[0],'');// worked with only first element
console.log(nestr);
console.log(mxml);
console.log(rattr);
この HTML サンプル ページについて
<div id="app" data-type="Application">
<div data-type="Label"></div>
<div data-type="Button"></div>
<div data-type="VBox"></div>
<div data-type="Group"></div>
</div>
その特定のことについて何か光がありますか?何かが足りないかもしれませんが、本当に手がかりがありません。それ以外の場合はここで尋ねてください。
表示する jsFiddle を作成しました。ブラウザのコンソールを開いて、結果を確認してください。
jsfiddle または私の正規表現のより良い説明について自由に答えてください。なぜ失敗するのですか。
フィードバックが得られるまで、テキストを置き換えることができるかどうかを確認し続けます.
前もって感謝します。