2017-11-21 17 views
-1

javascript変数の[counter] occurencesをすべて置き換える必要があります。 https://regex101.com/のようなオンラインのregexpツールを使用すると、私が望む[counter] -occurencesと一致するため、なぜ私はそれがうまく動作しないのかわかりません。Javascript大括弧の間にregexp文字列を置き換えます。

<div id="ITEM-9" data-widget-id="9" data-widget-type="CounterWidget" data-counter="6" data-icon="fa fa-bell" data-filterid="2" data-hidewhenzero="1" data-countervalue="[counter]" class="cust-masonry-item col-xs-6 col-sm-4 col-md-3 col-lg-3 cust-masonry-item-height-1 ng-scope" style="position: absolute; left: 74.9035%; top: 0px;"><div class="cust-masonry-item-content"><div class="cust-dashboard-counter"><span class="cust-dashboard-counter-icon"><span class="fa fa-bell"></span></span><span class="cust-dashboard-counter-title cust-readable-text">API</span><span class="cust-dashboard-counter-count"><span class="cust-dashboard-counter-number">[counter]</span><span class="cust-dashboard-counter-subscript cust-readable-text">&nbsp;Log messages to read</span></span></div></div></div> 

そして、私はこのような「置き換えという言葉で-strings [カウンター]を交換してみてください:

enter image description here

例の文字列に置換すること

var newHTML = element[0].outerHTML.toString().replace('/(\[counter\])/g', 'replaced'); 

期待結果:

<div id="ITEM-9" data-widget-id="9" data-widget-type="CounterWidget" data-counter="6" data-icon="fa fa-bell" data-filterid="2" data-hidewhenzero="1" data-countervalue="replaced" class="cust-masonry-item col-xs-6 col-sm-4 col-md-3 col-lg-3 cust-masonry-item-height-1 ng-scope" style="position: absolute; left: 74.9035%; top: 0px;"><div class="cust-masonry-item-content"><div class="cust-dashboard-counter"><span class="cust-dashboard-counter-icon"><span class="fa fa-bell"></span></span><span class="cust-dashboard-counter-title cust-readable-text">API</span><span class="cust-dashboard-counter-count"><span class="cust-dashboard-counter-number">replaced</span><span class="cust-dashboard-counter-subscript cust-readable-text">&nbsp;Log messages to read</span></span></div></div></div> 

はい、私は外側のHTMLに上記の文字列が含まれていると確信していますが、私の言葉は置き換えられません。

今私はとてもダムを感じています...正しい方向に向いてくれてありがとう。

+0

だまされやすい人ではありません[なぜ、このjavascriptの正規表現は動作しません?](http://stackoverflow.com/questions/7427731/why-this-javascript-regex-doesnt -作業)。 –

答えて

1

正規表現は、文字列の

newHTML = element[0].outerHTML.toString().replace(/(\[counter\])/g, "replaced"); 
+0

あなたの応答のためにこんにちは感謝しますが、私はSOのコードタグが私のダブル ""を削除したと思います。これはvar newHTML =要素[0] .outerHTML.toString()。replace( "/(\ [counter \])/ g"、response.data)です。私のコード – Verthosa

+1

で引用符が問題である場合は、それらをスキップしてください。正規表現は文字列ではない特別なオブジェクトです –

+0

ああ、私は今でも眠くなってしまいます。あなたの応答のためにたくさんのTnx、これは確かにそれを修正しました... – Verthosa

関連する問題