Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Regexp replace behaving strangely?

Reply
Thread Tools

Regexp replace behaving strangely?

 
 
Ciaran
Guest
Posts: n/a
 
      07-19-2009
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
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      07-19-2009
Ciaran wrote:
> 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');

--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
 
 
 
cronoklee
Guest
Posts: n/a
 
      07-19-2009
On Jul 19, 4:28*pm, Martin Honnen <(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
song
Guest
Posts: n/a
 
      07-20-2009
Ciaran <(E-Mail Removed)> writes:

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

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

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM
Why Are sring.assign & string.replace Behaving Abnormally? perseus C++ 1 07-17-2003 05:50 PM



Advertisments