简体   繁体   English

使用正则表达式提取数据

[英]Extract data using regular expression

I have these text fields. 我有这些文本字段。

<input type="text" value="<p>aaaaaaaaaaa</p>" id="tab_4_0_li_div_content"><input type="text" value="lab" id="tab_4_0_li_data"><input type="text" value="<p>dddd</p>" id="tab_4_1_li_div_content"><input type="text" value="client" id="tab_4_1_li_data">

Here tab_4_*_li_data is a <li> html of tab , and tab_4_0_li_div_content is id of <div> which will be show on <li> click. 这里tab_4_*_li_datatab_4_*_li_data<li> html,而tab_4_0_li_div_content<div> id ,将在<li>点击时显示。

Now I want to extract data from this input fields using regular expression. 现在,我想使用正则表达式从此输入字段中提取数据。 For example 例如

client,lab 

as key and 作为关键和

<p>aaaaaaaaaaa</p>,<p>dddd</p>

as value of key. 作为键的值。

If you see these are related to each others. 如果您看到它们彼此相关。

tab_4_0_li_div_content        tab_4_0_li_data
<p>aaaaaaaaaaa</p>             lab

tab_4_1_li_div_content        tab_4_1_li_data
<p>dddd</p>                    client

Div content part can content any thing, It's an text area. Div内容部分可以满足任何条件,这是一个文本区域。 So how we do this? 那么我们该怎么做呢?

There is no reason to have to use a regular expression to parse HTML. 没有理由必须使用正则表达式来解析HTML。 One thing, you are not going to have a good time going it. 一件事,您将不会有一个美好的时光。 Second, use the power of the DOM 第二,利用DOM的力量

 var contents = $("[id$=content]"); //find the elements that have an id that end with content var datas = $("[id$=data]"); //find the elements that have an id that end with data var details = {}; //object to hold results datas.each( function(i) { details[datas[i].value] = contents[i].value; }); //start looping and generate the object with the details you are after console.log(details); //Object {lab: "<p>aaaaaaaaaaa</p>", client: "<p>dddd</p>"} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text" value="<p>aaaaaaaaaaa</p>" id="tab_4_0_li_div_content"><input type="text" value="lab" id="tab_4_0_li_data"><input type="text" value="<p>dddd</p>" id="tab_4_1_li_div_content"><input type="text" value="client" id="tab_4_1_li_data"> 

Now that code assumes the data and content elements are in the same order, if not, than it would require a little processing, but it is not that hard to do. 现在,该代码假定数据和内容元素的顺序相同(如果不同),则需要进行少量处理,但这并不难。

  var datas = $("[id$=data]"); var details = {}; datas.each(function(i, elem) { var id = elem.id; var val = $("#" + id.replace("data", "div_content")).val(); details[elem.value] = val; }); console.log(details); //Object {lab: "<p>aaaaaaaaaaa</p>", client: "<p>dddd</p>"} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text" value="<p>aaaaaaaaaaa</p>" id="tab_4_0_li_div_content"> <input type="text" value="lab" id="tab_4_0_li_data"> <input type="text" value="<p>dddd</p>" id="tab_4_1_li_div_content"> <input type="text" value="client" id="tab_4_1_li_data"> 

You can do it like this: 您可以这样做:

function getValuesById(id1,id2){
    var vals = {};
    if(id1 === undefined)
        id1 = "\\d+";
    if(id2 === undefined)
        id2 = "\\d+";

    $("input").each(function (index, value) {
        console.log(new RegExp('tab_(' + id1 + '_' + id2 + ')_li_data'));
        var match = value.id.match(new RegExp('tab_(' + id1 + '_' + id2 + ')_li_data'));
        if (match) {
            vals[value.value] = $("#tab_" + match[1] + "_li_div_content").val();
        }
    });
    return vals;
}

Here I search through all input fields and match against tab_DIGITS_DIGITS_li_div_data and put that as a key and corresponding li_div_content as value in object values. 在这里,我搜索所有输入字段,并与tab_DIGITS_DIGITS_li_div_data匹配,并将其作为键,并将对应的li_div_content作为对象值的值。

Check it out here: JSFiddle 在这里查看: JSFiddle

UPDATE: I have updated the code to a function where you can send in a parameter to your two numerical values and will use any number if not supplied, ie use as getValuesById(4) for values like tab_4_*_li 更新:我已将代码更新为一个函数,您可以在其中向两个数值发送参数,并且将使用任何数字(如果未提供),即用作诸如tab_4_*_li类的值的getValuesById(4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM