Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex extract text between pattern

i need your help with a regex in javascript.

I've got an a-tag with follow pattern

<a href='#'>
  <!--:de-->some german text<!--:--><!--:en-->some english text<!--:-->
</a>

So, now i'm searching for a regular expression what extract me each part

 1. <!--:de-->some german text<!--:-->
 2. <!--:en-->some german text<!--:-->

Thanks VERY much in advance

like image 542
YeppThat'sMe Avatar asked Jun 16 '11 18:06

YeppThat'sMe


3 Answers

Try this:

var str = '<!--:de-->some german text<!--:--><!--:en-->some english text<!--:-->'; 
var match =  str.match(/de-->([^<]+).+?en[^>]+>([^<]+)/i);
var textInDe = match[1]; 
var textInEn = match[2]; 
like image 73
The Mask Avatar answered Oct 09 '22 10:10

The Mask


Try this Javascript code:

var ger = /<!--:de-->.*?<!--:-->/g;
document.writeln(html.match(ger).map(function (s) { return s; }));

var eng = /<!--:en-->.*?<!--:-->/g;
document.writeln(html.match(eng).map(function (s) { return s; }));

OUTPUT

some german text

some english text

like image 44
anubhava Avatar answered Oct 09 '22 11:10

anubhava


If you are expecting more than 2 parts, i would recommend you split the string by the delimiter

var str="<!--:de-->some german text<!--:--><!--:en-->some english text<!--:--><!--:fr-->some french text<!--:-->";

var col_array=str.split("<!--:-->");
var part_num=0;
while (part_num < col_array.length)
{
 document.write(col_array[part_num]+"<br>");
 part_num+=1;
}
like image 1
Adithya Surampudi Avatar answered Oct 09 '22 10:10

Adithya Surampudi