2011-12-07 13 views
0

ですべての行を置き換える:Javascriptを - 私はJavascriptブックマークレット意志作るしようとしているXの文字より少ない

  • (クラスの「mceContentBody」)フォームフィールドの内容を見て、
  • 検索をタグ内の内容が50文字未満の 未満のすべての段落タグ、および
  • "strong"タグを内部に追加します。

ので <p>This is less than 50 chars</p> ここ <p><strong>This is less than 50 chars</strong></p>

しかし <p>This is a very long line that is more than 50 characters so it will remain untouched.</p>

になる私が今持っているものですが、私はそれを実行すると、それは、フォームフィールドの内容全体を太字になります。

私は正規表現で何かを悩まされていると確信しています。私は何が欠けていますか?

javascript:var x = window.frames[1].document.getElementsByClassName("mceContentBody")[0].innerHTML; 

x=x.replace(/(<p.*?>([A-Za-z ]{0,50})<\/p>)/g, "<p><strong>$1</strong></p>"); 

window.frames[1].document.getElementsByClassName("mceContentBody")[0].innerHTML=x;empty(); 

ありがとうございます!

答えて

1
あなたはPの開始タグの末尾以外のすべてを一致させるように、それは、次の(または see this regex test)のようなものでなければなりませんこれにあなたの正規表現を変更し

:ここ

x=x.replace(/(<p[^>]*?>([A-Za-z ]{0,50})<\/p>)/g, "<p><strong>$1</strong></p>"); 

問題は、あなたがあまりにも一致しているということです多くは(this regex testを参照してください)。私が推測している良いサンプルHTMLは、あなたが問題を抱えているようなものです。

<form><p>This is my form it has a lot of words in this paragraph because it is too cool for school. This is my form it has a lot of words in this paragraph because it is too cool for school. This is my form it has a lot of words in this paragraph because it is too cool for school. This is my form it has a lot of words in this paragraph because it is too cool for school.</p><p>Short</p></form> 

注:これにはいくつかのミスがあります。何らかの理由でPの開始タグに ">"文字がある場合。私は、JavaScriptがインライン化されていない限り、それはそうではないことを前提としていました。

0

私はこれまで、あなたのコード(理由はコメントブロック内にある)を変更します

var x = window.frames[1].document.getElementsByClassName("mceContentBody")[0].innerHTML; 
/* 
    Changed: <p.*?> 
    To: <p[^>]*> 
    Because: "." will include ">". By making a negated character class, we are ensuring that the regex will find the closing ">". 

    Changed: [A-Za-z ]{0,50} 
    To: [^<]{1,50} 
    Because: Paragraph elements can contain other characters than letters and spaces (including your example paragraph to be captured. 
      Properly formated HTML should never have a "<" character in the innerHTML of a paragraph element. 
      Made the minimum "1" because there's no point to putting an empty strong element inside an empty paragraph element. 

     Removed outer capturing block as it was not being used. 

*/ 
x = x.replace(/<p[^>]*>([^<]{1,50})<\/p>/g, "<p><strong>$1</strong></p>"); 
window.frames[1].document.getElementsByClassName("mceContentBody")[0].innerHTML = x; 
empty(); 

JSLintはこれで持っている唯一の問題は否定文字クラスの使用が原因の「安全でない」とみなされていることですUnicode文字を取り込む可能性しかし、これを入力フィールドに使用していないので、それは問題ではありません。

これが役に立ちます。

+0

これは完全に動作します、ありがとうございます!残念ながら、私はあなたの応答をupvoteするのに十分な評判はありませんが、これはトリックでした! – mb6347

+0

私は助けてくれてうれしいです。 – pete

2

はちょうど手で磨かHTMLパーサを使用し、正規表現でHTMLを解析しないでください:

function replaceContents(contents) { 
var div = document.createElement("div"), 
    paragraphs, i, l, paragraph, text, 
    textProp = "textContent" in div ? "textContent" : "innerText"; 

div.innerHTML = contents; 

paragraphs = div.getElementsByTagName("p"); 
l = paragraphs.length; 

    for(i = 0; i < l; ++i) { 
    paragraph = paragraphs[i]; 
    text = paragraph[textProp]; 

     if(text.length > 0 && text.length < 50) { 
     paragraph.innerHTML = "<strong>"+text+"</strong>"; 
     } 
    } 

return div.innerHTML; 
} 

例ここで使用します。http://jsfiddle.net/wUfRQ/

0

あなたの外側の括弧はマッチ全体をキャプチャし、そう$1たくされていませんあなたは欲しい。代わりに$2を使用してください。

または外側の括弧を削除します。

関連する問題