jquery - javascript regex to find specific html tag details -


i want regex find out specific html tag details.

i tried bellow 2 regex :

<\s*tag[^>]*>(.*?)<\s*/\s*tag>  <tag[^<>]*>.+?<\/tag> 

bellow 2 test cases 1st regex :

in 1st example getting correct result in example 2 getting wrong result. in both cases in-puts same.

1st case : individual string , 2nd case : single string.

=================================== example 1 input =================================== <tagx>aaa</tagx> <tag>ggg</tag> <tag id="tag896">hhh</tag> <tagy>iii</tagy> <tag id="tag017">jjj</tag> <tag>kkk</tag> =================================== output 1 // correct =================================== <tag>ggg</tag> ggg <tag id="tag896">hhh</tag> hhh <tag id="tag017">jjj</tag> jjj <tag>kkk</tag> kkk   =================================== example 2 input (as single string) =================================== <tagx>aaa</tagx><tag>ggg</tag><tag id="tag896">hhh</tag><tagy>iii</tagy><tag id="tag017">jjj</tag><tag>kkk</tag> =================================== output 2 // wrong =================================== <tagx>aaa</tagx><tag>ggg</tag> aaa</tagx><tag>ggg  <tag id="tag896">hhh</tag> hhh  <tagy>iii</tagy><tag id="tag017">jjj</tag> iii</tagy><tag id="tag017">jjj  <tag>kkk</tag> kkk 

here want details of (tag) in 2nd case fetching (tag) + (tagx) + (tagy) details.

my input similar 2nd input...

its lil urgent... can solution this.

thanks...

your problem in regular expressions you've written, allow <tagx> (for example) opening tag if there's `' that's supposedly closes on same line.

your problem using regular expressions in case, might bad result if xml is:

<tag></tag> <tagx></tagx> <tag></tag> 

if tags inline, whole thing, careful.

i'd work (this works above example):

 <\s*tag((\s+[^<>]+\s*>)|(\s*>))[^<>]*<\s*\/tag\s*> 

here, allow whitespaces valid, don't allow nested tags, above example work. moreover, if allow nested tags, no regex work. @ example:

<tag> <tagx> <tag> </tag> </tagx> </tag> 

though, in example, <tag> <tagx> <tag> </tag> valid answer.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -