Ash Burlaczenko
Ash Burlaczenko

Reputation: 25465

Javascript: Regex code will not execute

Before I start I just want to say that I am completely new to Regex so please be gentle with me. Any comments about Regex in general will be greatly appreciated.

I have write the below code

var str = "<blah blah more <b>test</b>>";

var reg1 = "<(?!b>)(?!/b>)";
str = str.replace(new RegExp(reg1), "&lt;"); 

var reg2 = ">(?<!b>)(?<!/b>)";
str = str.replace(new RegExp(reg2), "&gt;"); 

alert(str);

I have checked the regex's using http://regexr.com?2toe2 and it does what I want it to which is to match any < or > but only if they are not html tags. 'I have only covered for now.

Now if your run this code, http://jsfiddle.net/ashburlaczenko/JdATY/9/ the alert is never executed. I put an alert after the first replace which displayed so the error is in the second stage.

Can anyone help me? Please remember these Regexs are my first attempt.

Thank you in advance.

Edit:

<blah blah more <b>test</b>><another <b>blah</b> blah <b>test</b>>

Should become

&lt;blah blah more <b>test</b>&gt;&lt;another <b>blah</b> blah <b>test</b>&gt;

Hope this is clearer.

Upvotes: 0

Views: 264

Answers (2)

Lee Kowalkowski
Lee Kowalkowski

Reputation: 11751

function escapeHtml($0, $1, $2, $3)
{
    var result = $1 == '<' ? '&lt;' + $2 + $3 : $1 + $2 + '&gt;';
  return result;
}

str = str.replace(/([<>])([^<>]*?)(\1)/g, escapeHtml);
str = str.replace(/^([^<]*?)>/, '$1&gt;');
str = str.replace(/<([^>]*?)$/, '&lt;$1');

This one works for the examples given.

The first replace does the following:

  • It looks for repetitions of < or > that don't have < or > in between them e.g. <...< or >...>.
  • The first bracket is $1, the text in between is $2, and the last bracket is $3.
  • If $1 is <, then $1 is escaped, otherwise $3 is escaped.

The second replace escapes > if it occurs near the start of the string without a < before it.

The third replace escapes < if it occurs near the end of the string without a > after it.

Upvotes: 1

Andy E
Andy E

Reputation: 344655

JavaScript doesn't support look-behind assertions in regular expressions. You can mimic it, with a little help from this blog post, but it's still not that great.

Upvotes: 2

Related Questions