开发者

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, n开发者_StackOverflow社区ow 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


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]; 


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


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜