Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


replace spaces in attribute with %20

Ok, here's my situation: I have an html page with a couple hundred "href" and "src" attributes all with image names containing spaces. Is there a way to replace spaces that are in "href" or "src" attributes in the file with %20 but leave all other spaces alone? I want to do this with regular expressions if possible so I can use it with find and replace in Sublime Text 2. So I need href="/images/image with space.png" to become href="/images/image%20with%20space.png" using a regular expression.

Edit: I was a bit unclear in my original post. I was looking for some sort of guide/tool/resource that could help me do this myself so that I'll know how in the future, not have someone write it for me.

2 Answers

James Barnett
James Barnett
39,199 Points

I don't personally use Sublime Text so I can't help you out with using the plugin. Seeing as this is a general web development question, maybe ask over on StackOverflow they are a great resource for such intermediate questions.

James Barnett
James Barnett
39,199 Points

HTML isn't a regular language and using regex with it is usually a bad idea.

Instead check out the String Encode plugin https://github.com/colinta/SublimeStringEncode

Thanks, I'll look into that!

I've been messing with String Encode and it looks like it does what I need but I can't get it to work. I select the path in my href attribute and have tried both the HTML Entitize and URL Encode options (from the command palette) but both do nothing, the spaces are still spaces and not %20.