Regex for searching tag id and javascript content

Hey, I'm trying to do something completely concrete with regex in javascript, and my regexp-foo is shaking at best. I wonder if there were any pros who could point me in the right direction. So I have a text ...

<item id="myid1">myitem1</item>
<item id="myid2">myitem2</item>

... etc.

And I would like to split it into an array that reads myid1, myitem1, myid2, myitem2, .... etc

There will never be nested elements, so the problem with recursive nesting does not exist. Anyone who can bash fast? Thank you for your help!

+3
source share
4 answers

There is a regular expression that will be:

  • id
  • html

: , . , .

<([^\s]+).*?id="([^"]*?)".*?>(.+?)</\1>

javascript :

search = '<item id="item1">firstItem</item><item id="item2">secondItem</item>';
regex = new RegExp(/<([^\s]+).*?id="([^"]*?)".*?>(.+?)<\/\1>/gi);
matches = search.match(regex);
results = {};
for (i in matches) {
    parts = regex.exec(matches[i]);
    results[parts[2]] = parts[3];
}

results , :

{
    "item1": "firstItem",
    "item2": "secondItem"
}

YMMV, <item> HTML.

+5

- Regex HTML id (, ), :

function GetTagByIdUsingRegex(tag,id,html) {
    return new RegExp("<" + tag + "[^>]*id[\\s]?=[\\s]?['\"]" + id + "['\"][\\s\\S]*?<\/" + tag + ">").exec(html);
}

, :

function GetTagByClassUsingRegex(tag,cls,html) {
    return new RegExp("<" + tag + "[^>]*class[\\s]?=[\\s]?['\"]" + cls + "[^'\"]*['\"][\\s\\S]*?<\/" + tag + ">").exec(html);
}
+1

:

http://www.pagecolumn.com/tool/regtest.htm

, :

(<[^>]+>)([^<]+)(<[^>]+>)

, JavaScript

RegExp:

var str = "<item id="myid1">myitem1</item><item id="myid2">myitem2</item><ssdad<sdasda><>dfsf";
var re = new RegExp("(<[^>]+>)([^<]+)(<[^>]+>)", "g");
var myArray = str.match(re);

:

var myArray = str.match(/(<[^>]+>)([^<]+)(<[^>]+>)/g)

if ( myArray != null) {
    for ( i = 0; i < myArray.length; i++ ) { 
        var result = "myArray[" + i + "] = " + myArray[i];
    }
}
0
source

This is an xml string. An XML parser seems the best for this task. Follow these steps:

var items = document.getElementsByTagName("item") ; //<> use the parent element if document is not
var dataArray = [ ] ;

for(var n = 0 ; n < items.length ; n++) {

     var id = items[n].id ;
     var text = items[n].childNodes[0] ;

         dataArray.push(id,text) ;

}

If your problem is that you cannot convert the xml string to an xml object, you will need to use the DOM parser :

var xmlString = "" ; //!! your xml string
var document = null ;

    if (window.ActiveXObject) { //!! for internet explorer

            document = new ActiveXObject("Microsoft.XMLDOM") ;
            document.async = "false" ;
            document.loadXML(xmlString) ;

    } else { //!! for everything else

        var parser = new DOMParser() ;
            document = parser.parseFromString(xmlString,"text/xml") ;

    }

Then use the above script.

0
source

Source: https://habr.com/ru/post/1755099/


All Articles