f



Javascript + Regex newbie question

Hi, I am relatively new to regex and have a simple question.

I want to find two words within a string (and everything in between to
the two words and replace it with something else.

The specific string is:

....[lots of
text]..................thumbnailUrl=xxxxxxxxxxxxxxxxx&..........................[more
text]



i want to snip out the everything in between and including

thumbnailUrl=

and the

&

and all the text in between the two words.  The text between the two
words is not a constant number and contains the % sign.

This is what I was thinking but its not working properly:

foo = foo.replace(/[thumbnailUrl=.*]-[&]/i, 'replacement text');

Can someone please help??

0
ygrewal (1)
4/14/2006 4:55:50 AM
comp.lang.javascript 38370 articles. 0 followers. javascript4 (1315) is leader. Post Follow

3 Replies
1198 Views

Similar Articles

[PageSpeed] 5

GCoder wrote:

>
> ...[lots of
> text]..................thumbnailUrl=xxxxxxxxxxxxxxxxx&..........................[more
> text]

>
> This is what I was thinking but its not working properly:
>
> foo = foo.replace(/[thumbnailUrl=.*]-[&]/i, 'replacement text');

I think you want

foo = foo.replace(/thumbnailUrl=.*?&/,"replacement text")

Maybe you still want the 'i' flag too if thumbnailUrl might be
capitalized differently.

- Peter

0
petermichaux
4/14/2006 6:14:08 AM
JRS:  In article <1144991557.121655.73250@e56g2000cwe.googlegroups.com>,
dated Thu, 13 Apr 2006 23:14:08 remote, seen in
news:comp.lang.javascript, petermichaux@gmail.com posted :
>
>foo = foo.replace(/thumbnailUrl=.*?&/,"replacement text")
>

 foo = foo.replace(/thumbnailUrl=[^?]*&/, "replacement text")

may be safer.
 
-- 
 � John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v4.00   IE 4 �
 <URL:http://www.jibbering.com/faq/>  JL/RC: FAQ of news:comp.lang.javascript 
 <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
 <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
0
Dr
4/14/2006 8:16:07 PM
Dr John Stockton wrote:

> [...] petermichaux@gmail.com posted :
>> foo = foo.replace(/thumbnailUrl=.*?&/,"replacement text")
> 
>  foo = foo.replace(/thumbnailUrl=[^?]*&/, "replacement text")
> 
> may be safer.

But not equivalent.  Peter proposed non-greedy matching.
Probably you meant 

  foo = foo.replace(/thumbnailUrl=[^&]*&/, "replacement text")


PointedEars
0
Thomas
4/16/2006 12:34:03 AM
Reply: