简体   繁体   中英

How do I replace every word in a body of text with another word?

How can I replace every word with another word and keep formatting such as line breaks, tabs, whitespace characters etc.?

I think the solution may be a RegEx but may also may need a function. I also want to match the length of the word. UPDATE I don't need to match the length of the word if I can get the the length of the word somehow. See the note at the end of this post.

Here is what I have so far in ActionScript 3. The JavaScript would be the same if you remove the strong typing of the variables.

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(" ");

}

Here is the input:

"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.

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().

Here is the output. If I enter the token, "bees" the result is:

bees bes bees bees bes bees bees bes bees bes bees bs bs bees bees bees b bees bees bees bes bs bes bees bs bes bees bees bs bs bees bs bees bees bes bs bees bees bes bees bees bees bes bs bees bees bees bes bs bes bees bs bes bees bees bees bees bs bes bees bes bees bees bs bes bees bees bees bees bees bs bes bes bs bees bees bees bees bs bees bees bs bees bees bees bes bes bees bees bees bees bees bees bes b bees bs bees bees bes bees bees bs bees bs bs bees bees bees bees bes bees bees bees bees bees bees bes bs bees bees bes bees bees bes bees bees bees bes bees bees bees

Notice the formatting and punctuation is missing. Actually, everything that is not a word is missing. I want to keep everything but replace every word with "bees" or another word I choose.

UPDATE:
Actually, can I do something like run a function on with RegEx replace? So a content.replace(/^\\W/g, myFunction)?

Or can I find all the words and replace them with the length of the word? So, "The quick brown fox jumps..." becomes, "3 5 5 3 4...". Then I can replace the numbers with my word?

This code:

   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);

Got the following output:

"bes bees bees bes bees bees bes bees bes" bs bs bees-bees bees—b bees bees bees bes bs bes bees bs bes bees. bs bs bees bs bees bees bes bs bees bees bes bees bees, bes bs bees bees bees bes bs bes bees bs bes bees bees. bees bs bes bees bes bees, bs bes bees bees bees.

bs bes bes bs bees, bees bees bs bees bees bs bees bees bees bes bes bees bees bees. bees bees bes b bees bs bees-bees bes bees, bs bees bs bs bees bees, bees bes bees =bees(), bes bs bees bees bes bees bees bes bees =bees.bes().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM