在JS中分解一个字符串

时间:2015-08-27 17:31:11

标签: javascript

我有一个脚本,可以随机从数组中获取引号,然后显示它。

我试图对其进行格式化,以便将引用和作者分开:

"插入引号"
报价的人姓名

我尝试使用\n<br />分割,即使在提醒时也无效。

这是我的代码:

//Initalize the array
var quotes = [];

//Insert data into the array
quotes[0] = "It doesn't matter how many times you have failed, you only have             to be right once." + "Mark Cuban";
quotes[1] = "Video games are bad for you? That's what they said about rock n' roll." + "Shigeru Miyamoto";
quotes[2] = "I'd like to be known as the person who saw things from a                           different point of view to others." + "Shigeru Miyamoto";
quotes[3] = "Stay hungry, stay foolish, stay crazy."  + "Steve Jobs";
quotes[4] = "The future was uncertain, absolutely, and there were many hurdles, twists, and turns to come, but as long as I kept moving forward, one foot in front of the other, the voices of fear and shame, the messages from those who wanted me to believe that I wasn't good enough, would be stilled." + "Chris Gardner";
quotes[5] = "Running a start-up is like eating glass. You just start to like the taste of your own blood." + "Sean Parker";
quotes[6] = "I used to drink cristal, the muh'fucker's racist. So I switched gold bottles on to that Spade shit" + "Shawn Carter (Jay Z)";
quotes[7] = "I think it's better to let my work do the talking" + "Shigeru Miyamoto.";
quotes[8] = "Success is a lousy teacher. It seduces smart people into thinking they can't lose." + "Bill Gates";
quotes[9] = "We need to reengineer companies to focus on figuring out who the customer is, what's the market and what kind of product you should build." + "Eric Ries";
quotes[10] = "I have no friends and no enemies - only competitors." + "Aristole Onassis";
quotes[11] = "Working 24 hours a day isn't enough anymore. You have to be willing to sacrifice everything to be successful, including your personal life, your family life, maybe more. If people think it's any less, they're wrong, and they will fail." + "Kevin O'Leary";
quotes[12] = "My hope is to the see the benefits of my labour spread out in the community." + "W. Brett Wilson";
quotes[13] = "I'm not here to make friends; I'm here to make money." + "Kevin O'Leary";
quotes[14] = "Good artists copy, great artists steal" + "Pablo Picasso";
quotes[15] = "Welcome ladies and gentlemen to the eighth wonder of the world. The flow of the century, always timeless; HOV!" + "Shawn Carter (Jay Z)";
quotes[16] = "Today’s “best practices” lead to dead ends; the best paths are new and untried." + "Peter Thiel";
quotes[17] = "I believe life is an intelligent thing: that things aren't random." + "Steve Jobs";
quotes[18] = "Pretty? You mean like rainbows, unicorns, and sparkles?" + "Michelle Brown";
quotes[19] = ".....and for that reason, I'm OUT!" +  "Mark Cuban";

//Splits the quote into two pieces, the quote and the person.

var quoteSplit = function (quotes) {
    var split = quotes.split("+").replace("\n");
}

//Displays a quote from the array at random.

var displayQuote = quotes[Math.floor(20 * Math.random())];
document.write(displayQuote);

//END

5 个答案:

答案 0 :(得分:3)

在构建数组时,您将引用与作者连接起来。所以这个:

quotes[0] = "It doesn't matter how many times you have failed, you only have to be right once." + "Mark Cuban";

此字符串设置为quotes[0]

结束
It doesn't matter how many times you have failed, you only have to be right once.Mark Cuban

并且您的split语句不起作用,因为+不包含在字符串中。但是,这不是设置阵列的好方法。如果您的报价包含+符号,会发生什么?

更好的方法是为每个项目创建一个对象:

quotes[0] = {
  text: "It doesn't matter how many times you have failed, you only have to be right once.",
  author: "Mark Cuban"
}

然后你可以这样做:

var displayQuote = quotes[Math.floor(20 * Math.random())];
document.write(displayQuote.text + '<br>' + displayQuote.author);

答案 1 :(得分:0)

似乎+符号不在您的字符串中。以下代码:

console.log("Today’s “best practices” lead to dead ends; the best paths are new and untried." + "Peter Thiel");

将返回给你的字符串

  

今天的“最佳实践”导致死路一条;最好的路径是新的和未经验证的。彼得·泰尔;

所以,你只需要在你的字符串中包含+ sig:

"Today’s “best practices” lead to dead ends; the best paths are new and untried.+Peter Thiel"

答案 2 :(得分:0)

正如Daniel A. White在评论部分所说。你正在考虑+成为字符串的一部分,但实际上你在每个索引上连接2个字符串。

quotes[3] = "Stay hungry, stay foolish, stay crazy."  + "Steve Jobs";

应该是:

quotes[3] = "Stay hungry, stay foolish, stay crazy.+Steve Jobs";

或者您可以使用正则表达式(不幸的是我现在无法提供正则表达式示例)但这些是您可能的两个选项。

答案 3 :(得分:0)

如果输出数组的任何元素,您将看到每个条目都是带引号和person的单个字符串。实施例

console.log(quotes[3]);
Stay hungry, stay foolish, stay crazy.Steve Jobs

那是因为+在应用于字符串时连接。

正如评论中所建议的那样,你可以在标点符号上使用拆分,虽然这会打破你的一些引号。

您可以执行类似

的操作
quotes[3]=["Stay hungry, stay foolish, stay crazy.","Steve Jobs"];

并分别输出每个元素。

答案 4 :(得分:0)

试试这个:

var quotes = {
  1: {
    quote: 'Hello world.',
    author: 'Test test'
  },
  2: {
    quote: 'Hello world 2.',
    author: 'Test test 2'
  },
};

// Display random quote
function displayQuote(){
  var key = Math.floor(Math.random() * Object.keys(quotes).length + 1);
  return quotes[key].quote + ' ' + quotes[key].author;
};

document.write(displayQuote());