Regexp replace behaving strangely?

C

Ciaran

Hi This replacement is doing funny things:

var stringObject='<p>[[:HTML some html code:]] </p>';
var patt1=new RegExp("\[\[:html(.*?):]]",'gi');
alert(stringObject.replace(patt1,"XXX"));

I want it to alert:
<p>XXX </p>

But it's alerting:
<p>[[:HTML some html codeXXX] </p>

Can anyone help me out?

Thanks,
Ciarán
 
M

Martin Honnen

Ciaran said:
Hi This replacement is doing funny things:

var stringObject='<p>[[:HTML some html code:]] </p>';
var patt1=new RegExp("\[\[:html(.*?):]]",'gi');
alert(stringObject.replace(patt1,"XXX"));

I want it to alert:
<p>XXX </p>

But it's alerting:
<p>[[:HTML some html codeXXX] </p>

Can anyone help me out?

Use a regular expression literal or make sure you double any backslashes
as you need one for the string literal and one for the regular
expression language:
var patt1=new RegExp("\\[\\[:html(.*?):]]",'gi');
 
C

cronoklee

Use a regular expression literal or make sure you double any backslashes
as you need one for the string literal and one for the regular
expression language:
   var patt1=new RegExp("\\[\\[:html(.*?):]]",'gi');

Great thanks - doubling the backslashes worked well!

Ciarán
 
S

song

var str = "<p>[[:HTML some html code:]]</p>";
var reg = /\[\[:html(.*?):\]\]/i;

console.log(str.replace(reg,"xxx"));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top