Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with a Regular Expression to use in the Match

Status
Not open for further replies.

edfimasa

Programmer
Apr 9, 2008
21
0
0
PT
Hello.
I use IE 6

I have the following code:
var flag = false;
var str = "aaaa\na <B>italico</B> e mais\n algo ee e e eee e e e e e a a a a a a a a a a a a a a a a a a a a ";
if(str.match(/<B>((.)*[\r\n]*(.)*)*<\/B>/i) != null) flag = true;
alert(flag);


When i run that in IE, it just freezes,and says "not responding".
I'm trying to find html tags within a text, in the example above im just trying to find if the text has the bold tag.

Can anyone help me, and explain me what's wrong?

Cheers
 
>I'm trying to find html tags within a text, in the example above im just trying to find if the text has the bold tag.
Simply do this.
[tt] var flag=/<B>.*?<\/B>/i.test(str);[/tt]
 
further notes
You see you're preoccupied with the presence of line-break etc, that is fair enough. In that case do rather this would suffice.
[tt] var flag=/<B>[\s\S]*?<\/B>/i.test(str);[/tt]
 
I see.

Thanks.


Just a question. What is doing the question mark symbol in
var flag=/<B>[\s\S]*?<\/B>/i.test(str);

Cheers.
 
? is to stop the reg expression being too greedy tending to match as far-reaching as possible. Without "?" qualifier, it would result in a single match all along the blue substring:
[tt] abc[blue]<b>123def\n string</b>[/blue]etc[/tt]
With "?" it would result in 2 matches:
[tt] abc[blue]<b>123def\nwwwhttp</b>[/blue]etcetc\n[blue]<b>another string</b>[/blue]etc[/tt]
But for establishing "existence", both would do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top