删除P5.js中特殊字符周围的空格

时间:2016-04-03 16:50:17

标签: javascript replace typography p5.js

我正在使用p5.js中的RiTa库从一首诗中制作一个“疯狂的lib”。为了正确输出字符,我使用了rita.tokenize函数使每个单词/字符成为数组中的值。然后我使用textWidth()来正确输出字符,在每个数组值周围添加“”,这样这些单词就不会变成这样。但是,通过这样做,我还在逗号,句号,问号等周围添加了额外的空格,看起来很奇怪。如何有选择地删除这些?我在JavaScript中查看它,但没有找到p5.js和RiTa的任何内容。有没有办法识别标点符号,然后使用布尔语句来纠正这个?或者我是否需要抛弃数组并使用字符串进行硬编码?感谢帮助! (请忽略我评论过的内容,我正在玩一些额外的文字)。克莱尔

//Generative Madlib poetry: Find a prose poetry text from Wikisource and 
//replace all nouns and adjectives with random nouns and adjectives. Try adding a rhymescheme.

var whatupwalt = ("What is it then between us? What is the count of the scores or hundreds of years between us? Whatever it is, it avails not, distance avails not, and place avails not, I too lived, Brooklyn of ample hills was mine, I too walkd the streets of Manhattan island, and bathed in the waters around it.");      
var words = [];
var lexicon;
var wordPosX = 10;
var wordPosY = 25;
var credit = ("- Walt Whitman");
//var click = ("Walt Whitman MadLibs! Click to switch up the poem");


function setup() {
  createCanvas(800,800);
  lexicon = new RiLexicon();


  textSize(20);
  fill(200,0, 20);



  words = RiTa.tokenize(whatupwalt);
  for(var i = 0; i < words.length; i++) {

  text(words[i], wordPosX, wordPosY);
  textWidth(words[i], 10);
  wordPosX += textWidth(words[i]+" ");
  text(credit, 600, 100);

  if(wordPosX > 700){
  wordPosX = 10;
  wordPosY += 20;
    }
  }
}

function mousePressed() {
  madLibs();

  textSize(12);
  textAlign(LEFT, TOP);

function madLibs() {
  var words = "What is " + 
    lexicon.randomWord("nn") + 
    " then between us? What is the " +
    lexicon.randomWord("nn") +
    " of the " + 
    lexicon.randomWord("nn") +
    " or hundreds of " +
    lexicon.randomWord("nn") +  
    " between us? Whatever " +
    lexicon.randomWord("nn") +
    " is, " +
    lexicon.randomWord("nn") +
    " avails not, " +
    lexicon.randomWord("nn") +
    " avails not, and " +
    lexicon.randomWord("nn") + 
    " avails not, I too lived, " +
    lexicon.randomWord("nn") +
    " of " +
    lexicon.randomWord("jj") + " " +
    lexicon.randomWord("nn") + 
    " was mine, I too walkd the " +
     lexicon.randomWord("nn") + 
     " of " +
    lexicon.randomWord("nn") + " " +
    lexicon.randomWord("nn") + 
    " , and bathed in the " +
    lexicon.randomWord("nn") + 
    " around " +
    lexicon.randomWord("nn") + "."


  background(255);
  textSize(20);
  text(words, 10, 10, 800-20, 800-20);
  credit = "-" + " " + lexicon.randomWord("nn") + " " + lexicon.randomWord("jj")
  text(credit, 580, 120);
  //rect(375, 240, 375, 220, 400, 240, 400, 220);
  //text(click, 400, 250)

2 个答案:

答案 0 :(得分:-1)

您可以使用简单的if语句连接它们时删除空格。像这样:

var wordOne = "one";
var wordTwo = ", three";

var combined;
if(wordTwo.startsWidth(",")){
   combined = wordOne + wordTwo;
}
else{
   combined = wordOne + " " + wordTwo;
}

您也可以使用正则表达式和replace()函数在创建String后修复它:

var wordOne = "one";
var wordTwo = ", three";
combined = wordOne + " " + wordTwo;
combined = combined.replace(/ ,/g, ',');

答案 1 :(得分:-1)

RiTa.untokenize()旨在处理这种情况。您只需将单词添加到数组中,然后使用RiTa.untokenize():

var words = ['one', ',', 'two', ',', 'three', '.'];

var result = RiTa.untokenize(words); 

console.log(result); // -> "one, two, three."