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

Removing Spaces around Special Characters in P5.js

我正在使用 p5.js 中的 RiTa 库从一首诗中制作 "mad lib"。为了正确输出字符,我使用 rita.tokenize 函数使每个 word/character 成为数组中的一个值。然后我使用 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)

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

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

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

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

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

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

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

var result = RiTa.untokenize(words); 

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