1

したがって、すべての1文字の「単語」(前置詞や接続詞など)を検索し、その後のスペースをaに置き換えて、 これらの文字が行末に残らないようにする置換関数は、次のようになる必要があることがわかりました。

myString.replace(/\s\w(?=\s)/,"$1 ")

Webページのすべてのテキストに適用するにはどうすればよいですか?<head>私はいくつかの普遍的な解決策を探しています。それは、Webページの他の何も変更する必要なしに私が入れた単純な機能です。

どうもありがとうございます。

4

3 に答える 3

4

あなたはこのようなことを試すことができます:

$('body').contents().filter(function(){ 
     return this.nodeType == 3;
}).each(function(){
   this.nodeValue = this.nodeValue.replace(/\s\w(?=\s)/,"$1&nbsp;");
});

Basically we grab the body element and get its contents - the contents() method will grab text nodes as well as elements which is what we want. Then we filter on nodeType reducing the set to only text nodes. then we do the regex replace.

于 2010-02-18T07:24:12.963 に答える
2
function replaceText(node)
{
  var current = node.nodeValue;
  var replaced = current.replace(searchpattern, replacepattern);
  node.nodeValue = replaced;
}
function traverse(node)
{
  var children = node.childNodes;
  var childLen = children.length;
  for(var i = 0; i < childLen; i++)
  {
    var child = children.item(i);
    if(child.nodeType == 3)//or if(child instanceof Text)
      replaceText(child);
    else
      traverse(child);
  }
}
function replaceAll()
{
  traverse(document.body);
}

replaceAll()体からの呼び出しonload

<body onload="replaceAll()">
于 2010-02-18T07:22:38.860 に答える
2
var str = 'When you want to change the DispFormUrl'.replace(/ /g, '&nbsp;');
alert(str);

result: "When you want to change the DispFormUrl"

于 2012-08-02T12:38:33.627 に答える