2014-10-12 115 views
0

如何将每个单词替换为另一个单词并保留格式,如换行符,制表符,空格字符等?如何用另一个单词替换文本正文中的每个单词?

我认为解决方案可能是RegEx,但也可能需要一个函数。我也想匹配这个词的长度。 UPDATE如果我能以某种方式获得单词的长度,我不需要匹配单词的长度。请参阅本文末尾的注释。

这是我到目前为止在ActionScript 3中的内容。如果删除变量的强类型,JavaScript将是相同的。

public function replaceText(content:String, token:String):String { 
    var words:Array = content ? content.split(/\W/g) : []; 
    var word:String; 
    var length:int = words.length; 
    var replacement:String; 
    var output:Array = []; 

    for (var i:int=0;i<length;i++) { 
     word = words[i]; 

     if (word.length==1) { 
      replacement = token.charAt(0); 
     } 
     else if (word.length==2 && token.length>1) { 
      replacement = token.charAt(0) + token.charAt(token.length-1); 
     } 
     else if (word.length==3 && token.length>2) { 
      replacement = token.charAt(0) + token.charAt(1) + token.charAt(token.length-1); 
     } 
     else if (word.length==4 && token.length>3) { 
      replacement = token.charAt(0) + token.charAt(1) + token.charAt(2) + token.charAt(token.length-1); 
     } 
     else { 
      replacement = token; 
     } 

     output.push(replacement); 
    } 

    return output.join(" "); 

} 

这里是输入:

“敏捷的棕色狐狸跳过懒狗”是包含了所有的英文字母的英文 全字母短句,一个短语。它 用于显示字体和测试打字机和计算机键盘, 和其他涉及英文字母中所有字母的应用程序。由于其简洁和一致性,它已成为广为人知的 。

在电脑的时代,这种普拉姆通常用于显示字体 样品和测试电脑键盘。 Microsoft Word有一个 命令,在命令= rand()以及Office 2007及更高版本中使用 command = rand.old()时,可以在Office 2003, 以上的版本中自动输入该句子。

这是输出。如果我进入令牌,“蜜蜂”的结果是:

蜜蜂BES蜜蜂蜜蜂BES蜜蜂蜜蜂BES蜜蜂BES蜜蜂BS BS蜜蜂蜜蜂 蜜蜂b蜜蜂蜜蜂蜜蜂BES BS BES蜜蜂BS BES蜜蜂蜜蜂BS BS蜜蜂BS 蜜蜂蜂BES BS蜜蜂蜂BES蜜蜂的蜜蜂的蜜蜂BES BS蜜蜂的蜜蜂的蜜蜂 BES BS BES蜜蜂BS BES蜜蜂的蜜蜂的蜜蜂的蜜蜂BS BES蜜蜂BES蜜蜂蜂 BS BES蜜蜂的蜜蜂的蜜蜂的蜜蜂的蜜蜂BS BES BES BS蜜蜂的蜜蜂的蜜蜂蜜蜂BS 蜜蜂蜂BS蜜蜂的蜜蜂的蜜蜂BES BES蜜蜂的蜜蜂的蜜蜂的蜜蜂的蜜蜂的蜜蜂BES b蜜蜂BS蜜蜂蜂BES蜜蜂蜂BS蜜蜂基地台BS1的蜜蜂的蜜蜂的蜜蜂的蜜蜂 BES蜜蜂的蜜蜂的蜜蜂的蜜蜂的蜜蜂的蜜蜂BES BS蜜蜂蜂BES蜜蜂蜂bes 蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂

注意格式和标点缺失。其实,所有不是一个字的东西都没有了。我想保留所有内容,但用“蜜蜂”或我选择的其他词替换每个词。

UPDATE:
其实,我可以做这样的事情上运行一个函数与正则表达式替换?所以一个content.replace(/^\ W/g,myFunction)?

或者我能找到所有的单词并用单词的长度替换它们吗?所以,“快速的棕色狐狸跳跃......”变成了“3 5 5 3 4 ...”。那么我可以用我的字代替数字?

回答

3

此代码:

var text = ' "The quick brown fox jumps over the lazy dog" is an English-language pangram—a phrase that contains all of the letters of the alphabet. It is used to show fonts and to test typewriters and computer keyboards, and in other applications involving all of the letters in the English alphabet. Owing to its brevity and coherence, it has become widely known.' 
    + "\n\n" + 
    ' In the age of computers, this pangram is commonly used to display font samples and for testing computer keyboards. Microsoft Word has a command to auto-type the sentence, in versions up to Office 2003, using the command =rand(), and in Office 2007 and later using the command =rand.old().' 
    + "\n"; 

    var token = 'bees'; 

    text = text.replace(/\b\w\b/g, token.charAt(0)); 
    text = text.replace(/\b\w{2}\b/g, token.charAt(0) + token.charAt(token.length-1)); 
    text = text.replace(/\b\w{3}\b/g, token.slice(0,2) + token.charAt(token.length-1)); 
    text = text.replace(/\b\w{4,}\b/g, token); 

    console.log(text); 

得到以下输出:

“BES蜜蜂蜂BES蜜蜂蜂BES蜜蜂BES” BS BS蜜蜂 - 蜜蜂蜂-B蜜蜂的蜜蜂的蜜蜂BES BS BES蜜蜂BS蜜蜂。 bs bs bes bes bes bes bes bes bes bes bes bes bes bees蜜蜂,bes bes bees bees bes bes bes bes bes bes蜜蜂。蜜蜂是蜜蜂蜜蜂,蜜蜂是蜜蜂蜜蜂。

BS BES百世BS蜜蜂,蜜蜂蜜蜂BS蜜蜂蜜蜂蜜蜂BS蜜蜂蜜蜂BES BES蜜蜂蜜蜂蜜蜂。蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂蜜蜂

+0

干得好,先生! – 2014-10-12 03:01:46

相关问题