gpt4 book ai didi

javascript - JS : Extracting Parts from a String (Paragraph) on variable length

转载 作者:行者123 更新时间:2023-12-02 15:56:14 25 4
gpt4 key购买 nike

My question is similar to this 但它有点复杂,而且我太菜鸟无法改变那里提供的方法。

我尝试过 substring 方法,但它不起作用,因为字符串的长度是可变的。

我有一个像这样的字符串:

Booking:
2 people

User Details:
Firstname Lastname
123456789
email@domain.com
facebook.com/username

Extras:
Service1
Service2

Pricing:
$1500/-

Comments:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus elementum ultricies pellentesque. Sed ullamcorper orci urna, et sagittis orci rhoncus quis.

Donec laoreet neque lectus, nec congue felis cursus non. Sed ac pulvinar nunc, vel cursus nulla. Curabitur at nisl ipsum. Etiam efficitur quam tortor, id malesuada lacus laoreet ac. Cras varius felis sem, id interdum enim accumsan et.

我需要将以下值存储为变量:

var people = 2
var name = firstname + lastname
var phone = 123456789
var email = email@domain.com
var fbook = facebook.com/username
var extras = Service1, Service2
var price = $1500
var comments = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus elementum ultricies pellentesque. Sed ullamcorper orci urna, et sagittis orci rhoncus quis.
Donec laoreet neque lectus, nec congue felis cursus non. Sed ac pulvinar nunc, vel cursus nulla. Curabitur at nisl ipsum. Etiam efficitur quam tortor, id malesuada lacus laoreet ac. Cras varius felis sem, id interdum enim accumsan et."

请记住,在某些情况下可能会缺少一些变量。即用户没有输入电子邮件和/或 Facebook URL,因此这些行可能为空,甚至缺少空换行符。

最佳答案

如果您发现正则表达式太复杂(即使您不是初学者,也很难做到正确),您可以使用更简单的 JavaScript,如下所示:

var input = "Booking:\n2 people\n\nUser Details:\nFirstname Lastname\n123456789\n\nfacebook.com/username\n\nExtras:\nService1\nService2\n\nPricing:\n$1500/-\n\nComments:\nLorem ipsum\n\ndolor sit amet";

// the input string is split into seperate lines and stored in array "lines":

var lines = input.split("\n");

// lines[0]="Booking:", lines[1]="2 people", lines[2]="", lines[3]="User Details" ...

// The lines are split per section, and stored in 2D-array "result":
// With expect=0 we look for sections[0], which is "Bookings".
// If the line "Bookings:" is found, "expect" is incremented to 1, so that
// we're now looking for sections[1], which is "User Details", and so on...
// If a line is found that is not the expected section title, and it's not empty,
// we add the line to the current section with push().

var sections = ["Booking", "User Details", "Extras", "Pricing", "Comments"];
var expect = 0, result = [];

for (var i = 0; i < lines.length; i++) {
if (lines[i] == sections[expect] + ":") result[expect++] = []
else if (result.length && lines[i] != "") result[result.length - 1].push(lines[i]);
}

// result[0][0]="2 people" (first line under "Booking")
// result[1][0]="Firstname Lastname" (first line under "User Details")
// result[1][1]="123456789" (second line under "User Details")
// result[1][2]="facebook.com/username" (second line under "User Details")
// ...
// result[4][0]="Lorem ipsum" (first line under "Comments")
// result[4][1]="dolor sit amet" (third line under "Comments", empty line is skipped)

// If all 5 sections have been found, we extract the variables:

var people, name, phone = "", email = "", fbook = "", extras = "", price, comments = "";

if (result.length == 5)
{

// people = the integer number at the beginning of the 1st line of the 1st section:

people = parseInt(result[0].shift());

// name = the 1st line of the 2nd section:

name = result[1].shift();

// The rest of the 2nd section is searched for the phone number, email and facebook.
// Because some of these lines may be missing, we cannot simply use the
// 1st line for phone, the 2nd line for email and the 3rd for facebook.

while (result[1].length) {
var temp = result[1].shift();
if (temp.search("facebook.com/") == 0) fbook = temp
else if (temp.search("@") > -1) email = temp
else phone = temp;
}

// All the lines in the 3rd section are added to string "extras".
// If the string is not empty, we put a comma between the parts:

while (result[2].length) {
if (extras.length) extras += ", ";
extras += result[2].shift();
}

// price = the floating-point number at the start of the 1st line of the 4th section:

price = parseFloat(result[3][0].substring(1));

// All the lines in the 5th section are added to string "comments".
// If the string is not empty, we put a newline between the parts:

while (result[4].length) {
if (comments.length) comments += "\n";
comments += result[4].shift();
}
}

alert("people: " + people + "\nname: " + name + "\nphone: " + phone + "\nemail: " + email + "\nfbook: " + fbook + "\nextras: " + extras + "\nprice: " + price + "\ncomments: " + comments);

关于javascript - JS : Extracting Parts from a String (Paragraph) on variable length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31530079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com