0

So I have a string that will have basically any kind of text in it and I want to capture all the text with regular expression and replace it.

var img_url = "http://grfx.domain.com/photos/dir/school/dir/m-dir/auto_original/123456.jpeg?123456";
img_url.replace(/regexp/,newvalue);

Just trying to capture everything in the img_url var and replace it.

Thanks!

kapa
  • 77,694
  • 21
  • 158
  • 175
Justin
  • 416
  • 3
  • 9
  • 26
  • Can you clarify what you mean. do you want to overwrite the contents with a new string or just replace part of it – Simon Laing Sep 20 '12 at 18:55
  • 1
    [Learn these first.](http://www.regular-expressions.info/) Then if you think you still want to do what you have just written using a regex use this `/^.*$/`. – Krzysztof Jabłoński Sep 20 '12 at 18:59

2 Answers2

7

Well, if you want to replace everything, you don't need regex. Simply write:

 img_url = newvalue;

If you are really determined to do this with a regular expression, this could help:

 img_url = img_url.replace(/.*/, newvalue);
kapa
  • 77,694
  • 21
  • 158
  • 175
1

If you're reassigning, just use =
If you want to replace all matches in one go, use the g flag, in which case this question is a duplicate of this post

If that expression needs to be dynamic:

var expr = new RegExp(replaceVar,'gi');//globally, and case-insensitively
var newUrl = oldUrl.replace(expr,replacement);

Note that, here, all back-slashes like you'd use for \b have to be escaped, too: \\b and \\/ for forward slashes. and \\\\ for an escaped backslash

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149