How to extract selective data from a textbox

时间:2019-04-17 02:32:52

标签: javascript

I have only a question with no sample code.

I am building a project management website for home inspections using html and javascript.

I get a standard email that comes with every new home inspection project assignment, and it is formatted the same with reliable row labels every time. In my email viewer, I'd Select All then Copy the email body and Paste into a textbox. When I submit the form with that textbox, I want to find the data on each row. A row might be "Floor type: Tile", and I want to assign a variable to "Tile" or whatever the data is for each row.

I have been doing this with an Excel macro written in Visual Basic, and in that code, I find certain characters or a string of characters then use + and - to get to the actual string I want. I have searched a lot but can't seem to phrase my needs correctly and have not found any answer for my situation.

Input would be pasted into a textbox... Type: Normal Date of Loss: 04/09/2019 Claim Number: XFP636

and then I'd like to set variables for example as follows:

$type = (Find the row with “Type:” and extract the string after the “:”) $dateofloss = (Find the row with “Date of Loss:” and extract the string after the “:”) $claimnumber = (Find the row with “Claim Number:” and extract the string after the “:”)

Thanks in advance for any help!

2 个答案:

答案 0 :(得分:0)

您在问题中添加了一个示例,因此在这里我将使用RegExp给出具体的答案:

let input = "Type: Normal Date of Loss: 04/09/2019 Claim Number: XFP636"

//type dateofloss claimnumber 

let type = input.match(/Type: (\w*)/)[1];
let re = new RegExp("Date of Loss: (\\d+/\\d+/\\d*)");
let dateofloss = input.match(re)[1];
let claimnumber = input.match(/Claim Number: (\w*)/)[1]

console.log(type, dateofloss, claimnumber);

OLD答案: 您没有指定任何输入或所需的输出,只是:

  

有人能帮助我至少指出正确的方向吗?

这就是我的观点:

您可以使用"".split()来获取值。

let input = "Type: Normal Date of Loss: 04/09/2019 Claim Number: XFP636";

//type dateofloss claimnumber Type: Normal Date of Loss: 04/09/2019 Claim Number: XFP636

let stuffs = ["Floor", "Roof", "Windows", "Bedrooms", "Living Rooms"];
let result = [];
for (let line of input.split("\n")) {
  for (let type of stuffs) {
    splitted = line.split(type + " type: ");
    if (splitted.length == 2) {
      result.push(splitted[1]);
    }
  }
}
console.log(result);

或者您可以使用Regrex:

let input = `Floor type: Tile
Roof type: Classic
Windows type: Double Insulate
Bedrooms type: Espacious
Living Rooms type: None`

let stuffs = ["Floor", "Roof", "Windows", "Bedrooms", "Living Rooms"];
let result = [];

for (let type of stuffs) {
  let re = new RegExp(type + " type: (\\w*)")
  let get = input.match(re);
  if (get.length == 2) {
    result.push(get[1]);
  }
}

console.log(result);

您可以使用字典而不是简单的数组来改善这一点,这样您就可以更好地了解每种类型。

答案 1 :(得分:0)

您需要更改格式的电子邮件并使用JSON格式对其进行格式化,然后将复制/粘贴到文本框中,然后可以使用JSON.parse()提取信息。