簡體   English   中英

使用正則表達式在選擇標簽 html 中查找字符串

[英]using regex expression to find a string within select tag html

var mydata = null;
var par = [];
var job = [] ;
var customer = [];
var optionObject = {};
var table=document.getElementById('table2'),tr,input,row,cell

var selectPosition = $('#num1').val();
$.post('php/GetJobs.php',{'selectPosition': selectPosition }, function(data) {
    mydata = data;
    par = JSON.parse(mydata); 
    for (var i= 0; i <= par.length-1; i++) {
        job.push(par[i][0]);
        customer.push(par[i][1]);
        //optionObject["JobNum"] = job[i];
        // optionObject["Customer"] = customer[i];          
    }

    for (var row = 0; row <= 0; row++) {
        tr=document.createElement('tr');
        tr.setAttribute("class","rows");
        tr.id = "AddedRow";
        i++;

        for(var cell=0; cell<1; cell++){
            var field = document.createElement("select");
            field.setAttribute("id","jobSelector"); 

            var jobCol=document.createElement('td');
            jobCol.id = "row";

            for(var i =0; i <= job.length-1; i++){
                var option = document.createElement("option");
                option.setAttribute("value", "");
                option.text = job[i] + ' ' +  customer[i];
                field.appendChild(option);
                //var textNode = document.createTextNode();
                //jobCol.appendChild(textNode);
            }        

            //field.setAttribute("option","Select Job");
            /* We can run a for loop here to obtain all the options from php call*/

            **var projectCol =document.createElement('td');
            projectCol.setAttribute("class",'celltimes4');
            projectCol.id = "row";
            projectCol.appendChild(field);   
            tr.appendChild(projectCol); 
            var text = field.options[field.selectedIndex].text;
            var regex = new RegExp(text )
            var textNode = document.createTextNode(field.options[field.selectedIndex].text);
            jobCol.appendChild(textNode);

            tr.appendChild(jobCol);
        }**
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        for(var j =0; j <=6; j++){
            var field = document.createElement("input");
            field.setAttribute("class","alignRight");
            field.setAttribute("type","text");
            field.setAttribute("name","hours");
            field.setAttribute("value","");
            td=document.createElement('td');
            td.setAttribute("class",'celltimes4');
            td.id = "row" + i;
            td.appendChild(field);
            tr.appendChild(td); 
        }

        td=document.createElement('td');
        td.setAttribute("class",'celltimes4c');
        td.id = "row14total";

        tr.appendChild(td);
        table.appendChild(tr);
    }
});

我知道所有這些代碼都不是必需的,但我一直在投票,因為我沒有提供完整的工作代碼,即使它真的對你們中的任何人都不起作用,因為我正在進行 PHP 調用和 JSON 解析。

但是,我的問題在於粗體代碼。 所以我想要實現的是獲取我的 select 語句中的內容,只提取作業字符串並在行中的第二秒使用它。 現在我如何擁有它,它需要 Select 語句的全部值並將其放在我的第二個單元格中。

所以我試圖使用正則表達式來取出字符串,但我根本不知道如何操作它。

您可以使用此正則表達式從選項中獲取文本: [\\d\\-,]+\\s+(.*)/

這里有一個示例,它在textPart中存儲了選項文本的文本部分。

 var text = "445930-1,2 Rivianna Foods"; var regex = /[\\d\\-,]+\\s+(.*)/; var textPart = text.match(regex)[1]; document.getElementById('result').innerHTML = textPart;
 "445930-1,2 Rivianna Foods" extracted text to: <pre id="result"></pre>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM