views:

65

answers:

2
var text = '#anything {behavior:url("csshover.htc");}'; //iam using " with url
text += "#anything {background:transparent url('img.png') no-repeat;}"; //iam using ' with url
text += "#anything {background-image:url('ok.jpg');}";

result

#anything {
    behavior:url("#");
}

#anything {
    background:transparent url('#') no-repeat;
}

#anything {
    background-image:url('#');
}

How can i do that ?

+1  A: 

Example: http://jsfiddle.net/GcsLQ/2/

var text = '#anything {behavior:url("csshover.htc");}'; //iam using " with url
text += "#anything {background:transparent url('img.png') no-repeat;}"; //iam using ' with url
text += "#anything {background-image:url('ok.jpg');}";


text = text.replace(/url\([^)]+\)/g, 'url("#")');

If you want the space formatting as well, do this:

Example: http://jsfiddle.net/GcsLQ/3/

var text = '#anything {behavior:url("csshover.htc");}'; //iam using " with url
text += "#anything {background:transparent url('img.png') no-repeat;}"; //iam using ' with url
text += "#anything {background-image:url('ok.jpg');}";


text = text.replace(/url\([^)]+\)/g, 'url("#")')
    .replace(/{/g,'{\n\t')
    .replace(/}/g,'\n}\n');

EDIT: Added quotes to the replaced URL.

patrick dw
+1  A: 

Replace # below with $2 if you want to get the URL with single quotes.

var text = '#anything {behavior:url("csshover.htc");}'; //iam using " with url
text += "#anything {background:transparent url('img.png') no-repeat;}"; //iam using ' with url
text += "#anything {background-image:url('ok.jpg');}";
text = text.replace(/url\((['"])(.+?)\1\)/g, "url('#')");
Lekensteyn
text = text.replace(/url\((['"])(.+?)\1\)/g, "url('#')");Thanks,This code helped me in my project.
faressoft
Your regex wipes out the `url()` part so you're just left with `'#'`.
patrick dw